|
| 1 | +import Sequelize from 'sequelize'; |
| 2 | +import {expect} from 'chai'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import helper from '../helper'; |
| 5 | +const { |
| 6 | + sequelize |
| 7 | +} = helper; |
| 8 | +import attributeFields from '../../../src/attributeFields'; |
| 9 | + |
| 10 | +import { |
| 11 | + GraphQLObjectType, |
| 12 | + GraphQLSchema, |
| 13 | + graphql |
| 14 | +} from 'graphql'; |
| 15 | + |
| 16 | +import { |
| 17 | + globalIdField |
| 18 | +} from 'graphql-relay'; |
| 19 | + |
| 20 | +import { |
| 21 | + sequelizeConnection |
| 22 | +} from '../../../src/relay'; |
| 23 | + |
| 24 | + |
| 25 | +describe('relay', function () { |
| 26 | + describe('connections', function () { |
| 27 | + before(function () { |
| 28 | + this.User = sequelize.define('user', {}, {timestamps: false}); |
| 29 | + this.Task = sequelize.define('task', {title: Sequelize.STRING}, {timestamps: false}); |
| 30 | + |
| 31 | + this.User.Tasks = this.User.hasMany(this.Task, {as: 'tasks', foreignKey: 'userId'}); |
| 32 | + |
| 33 | + this.taskType = new GraphQLObjectType({ |
| 34 | + name: this.Task.name, |
| 35 | + fields: { |
| 36 | + ...attributeFields(this.Task), |
| 37 | + id: globalIdField(this.Task.name) |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + this.spy = sinon.spy(options => options); |
| 42 | + |
| 43 | + this.viewerTaskConnection = sequelizeConnection({ |
| 44 | + name: 'Viewer' + this.Task.name, |
| 45 | + nodeType: this.taskType, |
| 46 | + target: this.User.Tasks, |
| 47 | + before: this.spy |
| 48 | + }); |
| 49 | + |
| 50 | + this.viewerType = new GraphQLObjectType({ |
| 51 | + name: 'Viewer', |
| 52 | + fields: { |
| 53 | + tasks: { |
| 54 | + type: this.viewerTaskConnection.connectionType, |
| 55 | + args: this.viewerTaskConnection.connectionArgs, |
| 56 | + resolve: this.viewerTaskConnection.resolve |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + this.schema = new GraphQLSchema({ |
| 62 | + query: new GraphQLObjectType({ |
| 63 | + name: 'RootQueryType', |
| 64 | + fields: { |
| 65 | + viewer: { |
| 66 | + type: this.viewerType, |
| 67 | + resolve: function (source, args, {viewer}) { |
| 68 | + return viewer; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + beforeEach(function () { |
| 77 | + this.sinon = sinon.sandbox.create(); |
| 78 | + |
| 79 | + this.viewer = this.User.build({ |
| 80 | + id: Math.ceil(Math.random() * 999) |
| 81 | + }); |
| 82 | + |
| 83 | + this.sinon.stub(this.Task, 'findAll').resolves([this.Task.build()]); |
| 84 | + this.sinon.stub(this.User, 'findById').resolves(this.User.build()); |
| 85 | + }); |
| 86 | + |
| 87 | + afterEach(function () { |
| 88 | + this.sinon.restore(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('passes context, root and info to before', async function () { |
| 92 | + await graphql(this.schema, ` |
| 93 | + query { |
| 94 | + viewer { |
| 95 | + tasks { |
| 96 | + edges { |
| 97 | + node { |
| 98 | + id |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + `, null, { |
| 105 | + viewer: this.viewer |
| 106 | + }); |
| 107 | + |
| 108 | + expect(this.spy).to.have.been.calledWithMatch( |
| 109 | + sinon.match.any, |
| 110 | + sinon.match({ |
| 111 | + orderBy: sinon.match.any |
| 112 | + }), |
| 113 | + sinon.match({ |
| 114 | + viewer: { |
| 115 | + id: this.viewer.id |
| 116 | + } |
| 117 | + }), |
| 118 | + sinon.match({ |
| 119 | + ast: sinon.match.any |
| 120 | + }) |
| 121 | + ); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments