Skip to content

Commit

Permalink
Merge pull request #83 from ButterCMS/add-get-series-to-tests
Browse files Browse the repository at this point in the history
Adds tests for series of GETs to test api / slug creation
  • Loading branch information
ViolanteCodes committed Jun 19, 2024
2 parents 7f9b5a4 + 29ee91b commit 0b8325c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
50 changes: 50 additions & 0 deletions __tests__/butter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ test(
await expect(response.data.slug).toEqual("example-news-page")
await expect(response.data.page_type).not.toEqual("sport")

// ensure we can get the page with the real page type attached
const response2 = await butter.page.retrieve('news', 'example-news-page')

await expect(response2.data.fields.headline).toEqual("This is an example news page");
await expect(response2.data.slug).toEqual("example-news-page")
await expect(response2.data.page_type).not.toEqual("sport")

return
}
);
Expand All @@ -43,6 +50,22 @@ test(
await expect(error.message).toEqual("TypeError: Failed to fetch");

}

try {
const singlePageResponse2 = await butter.page.retrieve(
"*/about",
"example-news-page",
{
"locale": "en",
"preview": 1
}
)
}
catch (error) {
await expect(error).toBeInstanceOf(Error);
await expect(error.message).toEqual("TypeError: Failed to fetch");

}

return

Expand All @@ -63,6 +86,33 @@ test(
await expect(firstPage).toHaveProperty('fields.title', 'This is a single page');

await expect(firstPage).not.toHaveProperty('date_time');

return
}
)

test(
"should list pages by single-pages and then retrieve a pageTyped page",
async () => {
const response = await butter.page.list('*')

await expect(response.meta.count).toEqual(2);
await expect(response.data).toHaveLength(2);

const firstPage = response.data[0];

await expect(firstPage).toHaveProperty('slug', 'single-page-1');
await expect(firstPage).toHaveProperty('fields.title', 'This is a single page');

await expect(firstPage).not.toHaveProperty('date_time');

const response2 = await butter.page.retrieve('news', 'example-news-page')

await expect(response2.data.fields.headline).toEqual("This is an example news page");
await expect(response2.data.slug).toEqual("example-news-page")
await expect(response2.data.page_type).not.toEqual("sport")

return
}
)

Expand Down
6 changes: 6 additions & 0 deletions mocks/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const failedSinglePostHandler = http.get(
() => HttpResponse.error("Failed to fetch")
)

const failedSinglePostWithGlobHandler = http.get(
"https://api.buttercms.com/v2/pages/*/about/fake-post-slug/",
() => HttpResponse.error("Failed to fetch")
)

const listPageHandlerError = http.get(
"https://api.buttercms.com/v2/pages/as/",
() => HttpResponse.json(
Expand All @@ -34,6 +39,7 @@ const listPageHandler = http.get(
export default [
singlePostHandler,
failedSinglePostHandler,
failedSinglePostWithGlobHandler,
listPageHandlerError,
listPageHandler,
];

0 comments on commit 0b8325c

Please sign in to comment.