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

Fix relayStylePagination to better support existing and incoming without edges property #9201

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
2 changes: 0 additions & 2 deletions src/cache/inmemory/__tests__/__snapshots__/policies.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,6 @@ Object {
"__typename": "PageInfo",
"endCursor": "YXJyYXljb25uZWN0aW9uOjI=",
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "YXJyYXljb25uZWN0aW9uOjI=",
},
"totalCount": 1292,
Expand Down Expand Up @@ -861,7 +860,6 @@ Object {
"__typename": "PageInfo",
"endCursor": "YXJyYXljb25uZWN0aW9uOjI=",
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "YXJyYXljb25uZWN0aW9uOjI=",
},
"totalCount": 1293,
Expand Down
7 changes: 0 additions & 7 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3220,13 +3220,6 @@ describe("type policies", function () {
ROOT_QUERY: {
__typename: "Query",
todos: {
edges: [],
pageInfo: {
"endCursor": "",
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "",
},
totalCount: 1292
},
}
Expand Down
100 changes: 99 additions & 1 deletion src/utilities/policies/__tests__/relayStylePagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,21 @@ describe('relayStylePagination', () => {
hasNextPage: true,
});
});

it("should not return empty edges if none existing", () => {
const resultWithTotalCount = policy.read!({
totalCount: 10
}, fakeReadOptions);

expect(
resultWithTotalCount,
).toEqual({
totalCount: 10,
});
});
});


describe('merge', () => {
const merge = policy.merge;
// The merge function should exist, make TS aware
Expand Down Expand Up @@ -143,7 +156,6 @@ describe('relayStylePagination', () => {
};
const result = merge(undefined, incoming, options);
expect(result).toEqual({
edges: [],
pageInfo: {
hasPreviousPage: false,
hasNextPage: true,
Expand Down Expand Up @@ -236,6 +248,92 @@ describe('relayStylePagination', () => {
expect(result).toEqual(fakeExisting);
})

describe('when incoming has no edges', () => {
it('should not replace existing null with empty edges', () => {
const fakeExisting = null;

const fakeIncoming = {
totalCount: 10
};

const fakeOptions = {
...options,
};

const result = merge(
fakeExisting,
fakeIncoming,
fakeOptions,
);

expect(result).toEqual({
totalCount: 10
});
})

it('should not merge existing with empty edges', () => {
const fakeExisting = {
totalCount: 10
};

const fakeIncoming = {
totalCount: 11
};

const fakeOptions = {
...options,
args: {
after: 'alpha',
},
};

const result = merge(
fakeExisting,
fakeIncoming,
fakeOptions,
);

expect(result).toEqual({
totalCount: 11
});
})
})

describe('when existing has no edges', () => {
it('should add incoming edges', () => {
const fakeExisting = {
totalCount: 10
};

const incomingEdges = [
{ cursor: 'alpha', node: makeReference("fakeAlpha") },
];
const incoming = {
edges: incomingEdges,
pageInfo: {
hasPreviousPage: false,
hasNextPage: true,
startCursor: 'alpha',
endCursor: 'alpha'
},
};
const fakeOptions = {
...options,
};

const result = merge(
fakeExisting,
incoming,
fakeOptions,
);

expect(result).toEqual({
...incoming,
totalCount: 10
});
})
})

it('should replace existing null with incoming', () => {
const incomingEdges = [
{ cursor: 'alpha', node: makeReference("fakeAlpha") },
Expand Down
Loading