Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/api.pact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,58 @@ describe("API Pact test", () => {
});
});
});

describe("deleting a product", () => {
it("ID 10 exists", async () => {
const expectedProduct = {
id: "10",
type: "CREDIT_CARD",
name: "28 Degrees",
};

await mockProvider
.addInteraction()
.given("a product with ID 10 exists")
.uponReceiving("a request to delete a product")
.withRequest("DELETE", "/product/10", (builder) => {
builder.headers({
Authorization: like("Bearer 2019-01-14T11:34:18.045Z"),
});
})
.willRespondWith(200, (builder) => {
builder.headers({
"Content-Type": "application/json; charset=utf-8",
});
builder.jsonBody(like(expectedProduct));
})
.executeTest(async (mockserver) => {
const api = new API(mockserver.url);
const product = await api.deleteProduct("10");

expect(product).toStrictEqual(new Product(expectedProduct));
return;
});
});

it("product does not exist", async () => {
await mockProvider
.addInteraction()
.given("a product with ID 11 does not exist")
.uponReceiving("a request to delete a product")
.withRequest("DELETE", "/product/11", (builder) => {
builder.headers({
Authorization: like("Bearer 2019-01-14T11:34:18.045Z"),
});
})
.willRespondWith(404)
.executeTest(async (mockserver) => {
const api = new API(mockserver.url);

await expect(api.deleteProduct("11")).rejects.toThrow(
"Request failed with status code 404"
);
return;
});
});
});
});
11 changes: 11 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export class API {
})
.then((r) => new Product(r.data));
}

deleteProduct(id: string): Promise<Product> {
return axios
.delete<ProductData>(`/product/${id}`, {
baseURL: this.baseURL,
headers: {
Authorization: this.generateAuthToken(),
},
})
.then((r) => new Product(r.data));
}
}

export default new API(import.meta.env.VITE_API_BASE_URL);
Loading