Skip to content

Commit

Permalink
feat(core): add projection merging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
binier committed Jul 30, 2020
1 parent 728f17b commit 1fa0e98
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion packages/core/test/query.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { query } from '../src';
import { query, EdgeBuilder } from '../src';

function edgesToObject(q: EdgeBuilder) {
return Object.entries(q['edges'])
.reduce((r, [k, v]) => {
if (v instanceof EdgeBuilder)
return { ...r, [k]: edgesToObject(v) };
r[k] = v;
return r;
}, {});
}

describe('Query test', () => {
it('should insert queryName in stringified query', () => {
Expand All @@ -13,4 +23,48 @@ describe('Query test', () => {
query('user').toString()
).toMatch(/q1\(.*\)/);
});

it('query.project should deep merge projections by default', () => {
const q = query()
.project({
a: 1,
b: {
c: 1,
d: 1,
},
})
.project({
a: 0,
b: {
c: 0,
},
});

expect(edgesToObject(q)).toMatchObject({
a: 0,
b: {
c: 0,
d: 1,
},
});
});

it('query.project should overwrite projections if flag is true', () => {
const q = query()
.project({
a: 1,
b: {
c: 1,
d: 1,
},
})
.project({
a: 0,
}, true);

const edges = edgesToObject(q);

expect(edges.a).toEqual(0);
expect(edges.b).toEqual(undefined)
});
});

0 comments on commit 1fa0e98

Please sign in to comment.