|
| 1 | +const runSift = require(`../run-sift`) |
| 2 | +const { |
| 3 | + GraphQLObjectType, |
| 4 | + GraphQLNonNull, |
| 5 | + GraphQLID, |
| 6 | + GraphQLString, |
| 7 | +} = require(`graphql`) |
| 8 | +const { connectionFromArray } = require(`graphql-skip-limit`) |
| 9 | + |
| 10 | +jest.mock(`../node-tracking`, () => { |
| 11 | + return { |
| 12 | + trackInlineObjectsInRootNode: () => jest.fn(), |
| 13 | + } |
| 14 | +}) |
| 15 | + |
| 16 | +jest.mock(`../../redux/actions/add-page-dependency`, () => { |
| 17 | + return { |
| 18 | + createPageDependency: jest.fn(), |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +const mockNodes = [ |
| 23 | + { |
| 24 | + id: `id_1`, |
| 25 | + string: `foo`, |
| 26 | + }, |
| 27 | + { |
| 28 | + id: `id_2`, |
| 29 | + string: `bar`, |
| 30 | + }, |
| 31 | + { |
| 32 | + id: `id_3`, |
| 33 | + string: `baz`, |
| 34 | + }, |
| 35 | +] |
| 36 | + |
| 37 | +jest.mock(`../../redux`, () => { |
| 38 | + return { |
| 39 | + getNode: id => mockNodes.find(node => node.id === id), |
| 40 | + } |
| 41 | +}) |
| 42 | + |
| 43 | +describe(`run-sift`, () => { |
| 44 | + const typeName = `test` |
| 45 | + const type = new GraphQLObjectType({ |
| 46 | + name: typeName, |
| 47 | + fields: () => { |
| 48 | + return { |
| 49 | + id: new GraphQLNonNull(GraphQLID), |
| 50 | + string: GraphQLString, |
| 51 | + } |
| 52 | + }, |
| 53 | + }) |
| 54 | + const nodes = mockNodes |
| 55 | + |
| 56 | + describe(`filters by just id correctly`, () => { |
| 57 | + it(`eq operator`, async () => { |
| 58 | + const args = { |
| 59 | + filter: { |
| 60 | + id: { eq: `id_2` }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + const resultSingular = await runSift({ |
| 65 | + type, |
| 66 | + nodes, |
| 67 | + typeName, |
| 68 | + args, |
| 69 | + connection: false, |
| 70 | + }) |
| 71 | + |
| 72 | + const resultConnection = await runSift({ |
| 73 | + type, |
| 74 | + nodes, |
| 75 | + typeName, |
| 76 | + args, |
| 77 | + connection: true, |
| 78 | + }) |
| 79 | + delete resultConnection.totalCount |
| 80 | + |
| 81 | + expect(resultSingular).toEqual(nodes[1]) |
| 82 | + expect(resultConnection).toEqual(connectionFromArray([nodes[1]], args)) |
| 83 | + }) |
| 84 | + |
| 85 | + it(`non-eq operator`, async () => { |
| 86 | + const args = { |
| 87 | + filter: { |
| 88 | + id: { ne: `id_2` }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + const resultSingular = await runSift({ |
| 93 | + type, |
| 94 | + nodes, |
| 95 | + typeName, |
| 96 | + args, |
| 97 | + connection: false, |
| 98 | + }) |
| 99 | + |
| 100 | + const resultConnection = await runSift({ |
| 101 | + type, |
| 102 | + nodes, |
| 103 | + typeName, |
| 104 | + args, |
| 105 | + connection: true, |
| 106 | + }) |
| 107 | + delete resultConnection.totalCount |
| 108 | + |
| 109 | + expect(resultSingular).toEqual(nodes[0]) |
| 110 | + expect(resultConnection).toEqual( |
| 111 | + connectionFromArray([nodes[0], nodes[2]], args) |
| 112 | + ) |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments