Skip to content

Commit

Permalink
[contract-tests] fix: Adding delete scenario (#56)
Browse files Browse the repository at this point in the history
* [contract-tests] fix: Adding delete scenario
  • Loading branch information
helloitsdave authored Jun 16, 2024
1 parent 49549a4 commit a37ffd2
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 104 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/backend-contract-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches: [ "main" ]
paths:
- "backend/**"
- "frontend/src/api/**"
- "frontend/contract-tests/**"

defaults:
run:
Expand All @@ -17,7 +19,7 @@ jobs:
API_URL: http://localhost:5000
CONTRACT_TEST_AUTH_TOKEN: ${{ secrets.CONTRACT_TEST_AUTH_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
Expand Down
217 changes: 115 additions & 102 deletions frontend/contract-tests/consumer.pact.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import path from "path";
import path from 'path';
import {
PactV3,
MatchersV3,
SpecificationVersion,
} from "@pact-foundation/pact";
import * as API from "../src/api/apiService";
import { AxiosResponse } from "axios";
} from '@pact-foundation/pact';
import * as API from '../src/api/apiService';
import { AxiosResponse } from 'axios';
const { eachLike, like } = MatchersV3;

const provider = new PactV3({
consumer: "NotesFEService",
provider: "NotesBEService",
logLevel: "debug",
dir: path.resolve(process.cwd(), "pacts"),
consumer: 'NotesFEService',
provider: 'NotesBEService',
logLevel: 'debug',
dir: path.resolve(process.cwd(), 'pacts'),
spec: SpecificationVersion.SPECIFICATION_VERSION_V2,
host: "127.0.0.1",
host: '127.0.0.1',
port: 5000,
});

describe("Pact with NotesBEService", () => {
it("returns the users notes", async () => {
describe('Pact with NotesBEService', () => {
it('returns the users notes', async () => {
provider.addInteraction({
states: [{ description: "notes are returned" }],
uponReceiving: "a request to get notes",
states: [{ description: 'notes are returned' }],
uponReceiving: 'a request to get notes',
withRequest: {
method: "GET",
path: "/api/notes",
method: 'GET',
path: '/api/notes',
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: eachLike({
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "Note Title",
content: "Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'Note Title',
content: 'Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
}),
},
});
Expand All @@ -48,147 +48,160 @@ describe("Pact with NotesBEService", () => {

expect(response.data).toStrictEqual([
{
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "Note Title",
content: "Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'Note Title',
content: 'Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
},
]);
});
});

it("add a note", async () => {
it('add a note', async () => {
provider.addInteraction({
states: [{ description: "note is added" }],
uponReceiving: "a request to create a note",
states: [{ description: 'note is added' }],
uponReceiving: 'a request to create a note',
withRequest: {
method: "POST",
path: "/api/notes",
method: 'POST',
path: '/api/notes',
body: {
title: "New Note Title",
content: "New Note Content",
title: 'New Note Title',
content: 'New Note Content',
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: like ({
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "New Note Title",
content: "New Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
body: like({
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'New Note Title',
content: 'New Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
}),
},
});

await provider.executeTest(async (mockService) => {
const response = await API.postNote({
title: "New Note Title",
content: "New Note Content",
});
title: 'New Note Title',
content: 'New Note Content',
});

expect(response.data).toStrictEqual(
{
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "New Note Title",
content: "New Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
},
);
expect(response.data).toStrictEqual({
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'New Note Title',
content: 'New Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
});
});
});

it("update a note", async () => {
it('update a note', async () => {
provider.addInteraction({
states: [{ description: "note is updated" }],
uponReceiving: "a request to update a note",
states: [{ description: 'note is updated' }],
uponReceiving: 'a request to update a note',
withRequest: {
method: "PUT",
path: "/api/notes/a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
method: 'PUT',
path: '/api/notes/a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
body: {
title: "Updated Note Title",
content: "Updated Note Content",
title: 'Updated Note Title',
content: 'Updated Note Content',
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: like ( {
"id": "a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
"title": "Updated Note Title",
"content": "Updated Note Content",
"createdAt": "2024-05-21T22:58:55.743Z",
"updatedAt": "2024-05-26T19:43:58.742Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
}),
body: like({
id: 'a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
createdAt: '2024-05-21T22:58:55.743Z',
updatedAt: '2024-05-26T19:43:58.742Z',
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
}),
},
});

await provider.executeTest(async (mockService) => {
const response = await API.patchNote({
id: "a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
title: "Updated Note Title",
content: "Updated Note Content",
id: 'a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
});

expect((response as AxiosResponse<any>).data).toStrictEqual(
{
"id": "a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
"title": "Updated Note Title",
"content": "Updated Note Content",
"createdAt": "2024-05-21T22:58:55.743Z",
"updatedAt": "2024-05-26T19:43:58.742Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
);
expect((response as AxiosResponse<any>).data).toStrictEqual({
id: 'a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
createdAt: '2024-05-21T22:58:55.743Z',
updatedAt: '2024-05-26T19:43:58.742Z',
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
});
});
});
it("update non-existant note", async () => {
it('update non-existant note', async () => {
provider.addInteraction({
states: [{ description: "note does not exist" }],
uponReceiving: "a request to update a non-existant note",
states: [{ description: 'note does not exist' }],
uponReceiving: 'a request to update a non-existant note',
withRequest: {
method: "PUT",
path: "/api/notes/b37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
method: 'PUT',
path: '/api/notes/b37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
body: {
title: "Updated Note Title",
content: "Updated Note Content",
title: 'Updated Note Title',
content: 'Updated Note Content',
},
},
willRespondWith: {
status: 404,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: like ( { error: "Note not found" }),
body: like({ error: 'Note not found' }),
},
});

await provider.executeTest(async (mockService) => {
const response = await API.patchNote({
id: "b37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
title: "Updated Note Title",
content: "Updated Note Content",
id: 'b37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
});

expect(response.status).toBe(404);

expect(response).toStrictEqual(
{
error: "Note not found",
status: 404,
expect(response).toStrictEqual({
error: 'Note not found',
status: 404,
});
});
});
it('delete a note', async () => {
provider.addInteraction({
states: [{ description: 'note is deleted' }],
uponReceiving: 'a request to delete a note',
withRequest: {
method: 'DELETE',
path: '/api/notes/0a97f1c3-294e-43e8-b78f-60209e972ee9',
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json',
},
);
body: like({
status: 'ok',
}),
},
});
});
});
2 changes: 1 addition & 1 deletion frontend/pacts/NotesFEService-NotesBEService.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
],
"metadata": {
"pact-js": {
"version": "12.5.0"
"version": "12.5.2"
},
"pactRust": {
"ffi": "0.4.20",
Expand Down

1 comment on commit a37ffd2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

96.91%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
src
   App.tsx87.50%75%96%30–31, 40–41
   NoteApp.tsx94.44%100%97.04%72–76
src/api
   apiService.ts87.50%85.71%88%100, 40, 40–45, 94–99
src/components
   Header.tsx100%100%100%
   Login.tsx81.82%100%98.72%32, 32–33
   Note.tsx100%100%100%
   NoteForm.tsx100%100%100%
   NoteFormModal.tsx100%100%100%
   NoteGrid.tsx100%100%100%
   RegistrationForm.tsx86.67%100%97.04%57, 57–61
   RegistrationLink.tsx100%100%100%
   Spinner.tsx100%100%100%

Please sign in to comment.