Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for all returned posts in the post type handler instead of just the first one #866

Merged
merged 4 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-socks-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frontity/wp-source": patch
---

Fix a bug where frontity could not fetch a page or child page with the same slug as the parent/child.
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,119 @@ Object {
}
`;

exports[`page can have a parent with the same slug 1`] = `
Object {
"api": "https://test.frontity.org/wp-json/",
"attachment": Object {},
"author": Object {
"1": Object {
"id": 1,
"link": "/author/author-1/",
"name": "Author 1",
"slug": "author-1",
},
},
"authorBase": "",
"category": Object {},
"categoryBase": "",
"data": Object {
"/author/author-1/": Object {
"id": 1,
"isFetching": false,
"isReady": false,
"link": "/author/author-1/",
"page": 1,
"query": Object {},
"route": "/author/author-1/",
},
"/page-1/": Object {
"id": 2,
"isFetching": false,
"isPage": true,
"isPostType": true,
"isReady": true,
"link": "/page-1/",
"page": 1,
"query": Object {},
"route": "/page-1/",
"type": "page",
},
"/parent/page-1/": Object {
"id": 1,
"isFetching": false,
"isPage": true,
"isPostType": true,
"isReady": true,
"link": "/parent/page-1/",
"page": 1,
"query": Object {},
"route": "/parent/page-1/",
"type": "page",
},
},
"entity": [Function],
"get": [Function],
"homepage": "",
"isWpCom": false,
"page": Object {
"1": Object {
"_embedded": Object {
"author": Array [
1,
],
"up": Array [
Object {
"id": 2,
"link": "https://test.frontity.org/parent/",
"slug": "parent",
},
],
},
"author": 1,
"featured_media": 1,
"id": 1,
"link": "/parent/page-1/",
"parent": 2,
"slug": "page-1",
"type": "page",
},
"2": Object {
"_embedded": Object {
"author": Array [
1,
],
},
"author": 1,
"featured_media": 1,
"id": 2,
"link": "/page-1/",
"parent": 0,
"slug": "page-1",
"type": "page",
},
},
"params": Object {},
"post": Object {},
"postEndpoint": "posts",
"postTypes": Array [
Object {
"archive": "/cpt",
"endpoint": "cpts",
"type": "cpt",
},
],
"postsPage": "",
"redirections": "no",
"subdirectory": "",
"tag": Object {},
"tagBase": "",
"taxonomies": Array [],
"taxonomy": Object {},
"type": Object {},
"url": "https://test.frontity.org",
}
`;

exports[`page doesn't exist in source.page 1`] = `
Object {
"api": "https://test.frontity.org/wp-json/",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"id": 2,
"slug": "page-1",
"type": "page",
"link": "https://test.frontity.org/page-1/",
"author": 1,
"parent": 0,
"featured_media": 1,
"_embedded": {
"author": [
{
"id": 1,
"name": "Author 1",
"link": "https://test.frontity.org/author/author-1/",
"slug": "author-1"
}
]
}
},
{
"id": 1,
"slug": "page-1",
"type": "page",
"link": "https://test.frontity.org/parent/page-1/",
"author": 1,
"parent": 2,
"featured_media": 1,
"_embedded": {
"author": [
{
"id": 1,
"name": "Author 1",
"link": "https://test.frontity.org/author/author-1/",
"slug": "author-1"
}
],
"up": [
{
"id": 2,
"link": "https://test.frontity.org/parent/",
"slug": "parent"
}
]
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { mockResponse } from "./mocks/helpers";
import attachment1 from "./mocks/post-type/attachment-1.json";
import page1 from "./mocks/post-type/page-1.json";
import page1WithParent from "./mocks/post-type/page-1-with-parent.json";
import childPageWithParentSameSlug from "./mocks/post-type/child-page-with-parent-same-slug.json";
import post1 from "./mocks/post-type/post-1.json";
import post1Revision from "./mocks/post-type/post-1-revision.json";
import post1withType from "./mocks/post-type/post-1-with-type.json";
Expand Down Expand Up @@ -280,6 +281,28 @@ describe("page", () => {
expect(postTypeHandler).toHaveBeenCalled();
expect(store.state.source).toMatchSnapshot();
});

test("can have a parent with the same slug", async () => {
// Mock Api responses
api.get = jest
.fn()
.mockResolvedValueOnce(mockResponse(childPageWithParentSameSlug));

await store.actions.source.fetch("/parent/page-1/");
const childData = store.state.source.data["/parent/page-1/"];
expect(childData.isReady).toBe(true);

// at this point both /parent/page-1 and /page-1 should already be in state.
const parentData = store.state.source.data["/page-1/"];
expect(parentData.link).toBe("/page-1/");

// however it's not "ready" yet.
await store.actions.source.fetch("/page-1/");
expect(parentData.isReady).toBe(true);

expect(postTypeHandler).toHaveBeenCalled();
expect(store.state.source).toMatchSnapshot();
});
});

describe("attachment", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/wp-source/src/libraries/handlers/postType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const postTypeHandler = ({
if (populated.length > 0) {
// We have to check if the link property in the data that we
// populated is the same as the current route.
if (populated[0].link === route) {
if (populated.some((post) => post.link === route)) {
isHandled = true;
isMismatched = false;
matchedEndpoint = endpoint;
Expand Down