Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mercedesb committed Feb 26, 2020
1 parent 9291c5b commit 16d731b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ async function fetchData(url, method, data) {
body: !!data ? JSON.stringify(data) : null,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": origin
Accept: "application/json"
}
})
.then(response => {
Expand Down
54 changes: 36 additions & 18 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ let mockResponse = {};
let mockJsonPromise = Promise.resolve(mockResponse);
let mockFetchPromise;

afterEach(() => {
global.fetch.mockClear();
afterAll(() => {
delete global.fetch;
});

Expand All @@ -33,10 +32,14 @@ describe("useApi", () => {
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
});

it("calls fetch with the expected parameters", done => {
it("returns the expected response", () => {
const { get } = useApi();
get(url);
expect(get(url)).resolves.toEqual(mockResponse);
});

it("calls fetch with the expected parameters", async done => {
const { get } = useApi();
await get(url);
expect(global.fetch).toHaveBeenCalledWith(
url,
expect.objectContaining({
Expand Down Expand Up @@ -88,10 +91,10 @@ describe("useApi", () => {
});

xdescribe("with an onUnauthorized handler passed in", () => {
it("calls the unauthorizedHandler", async () => {
it("calls the unauthorizedHandler", () => {
const onUnauthorized = jest.fn;
const { get } = useApi(onUnauthorized);
await get(url);
get(url);
expect(onUnauthorized).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -136,9 +139,14 @@ describe("useApi", () => {
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
});

it("calls fetch with the expected parameters", done => {
it("returns the expected response", () => {
const { post } = useApi();
expect(post(url, data)).resolves.toEqual(mockResponse);
});

it("calls fetch with the expected parameters", async done => {
const { post } = useApi();
post(url, data);
await post(url, data);

expect(global.fetch).toHaveBeenCalledWith(
url,
Expand Down Expand Up @@ -192,10 +200,10 @@ describe("useApi", () => {
});

xdescribe("with an onUnauthorized handler passed in", () => {
it("calls the unauthorizedHandler", async () => {
it("calls the unauthorizedHandler", () => {
const onUnauthorized = jest.fn;
const { post } = useApi(onUnauthorized);
await post(url, data);
post(url, data);
expect(onUnauthorized).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -240,9 +248,14 @@ describe("useApi", () => {
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
});

it("calls fetch with the expected parameters", done => {
it("returns the expected response", () => {
const { put } = useApi();
put(url, data);
expect(put(url, data)).resolves.toEqual(mockResponse);
});

it("calls fetch with the expected parameters", async done => {
const { put } = useApi();
await put(url, data);

expect(global.fetch).toHaveBeenCalledWith(
url,
Expand Down Expand Up @@ -296,10 +309,10 @@ describe("useApi", () => {
});

xdescribe("with an onUnauthorized handler passed in", () => {
it("calls the unauthorizedHandler", async () => {
it("calls the unauthorizedHandler", () => {
const onUnauthorized = jest.fn;
const { put } = useApi(onUnauthorized);
await put(url, data);
put(url, data);
expect(onUnauthorized).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -343,9 +356,14 @@ describe("useApi", () => {
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
});

it("calls fetch with the expected parameters", done => {
it("returns the expected response", () => {
const { del } = useApi();
expect(del(url)).resolves.toEqual(mockResponse);
});

it("calls fetch with the expected parameters", async done => {
const { del } = useApi();
del(url);
await del(url);

expect(global.fetch).toHaveBeenCalledWith(
url,
Expand Down Expand Up @@ -398,10 +416,10 @@ describe("useApi", () => {
});

xdescribe("with an onUnauthorized handler passed in", () => {
it("calls the unauthorizedHandler", async () => {
it("calls the unauthorizedHandler", () => {
const onUnauthorized = jest.fn;
const { del } = useApi(onUnauthorized);
await del(url);
del(url);
expect(onUnauthorized).toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 16d731b

Please sign in to comment.