|
| 1 | +import { ApolloServer } from '@apollo/server'; |
| 2 | +import gql from 'graphql-tag'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { resolvers, typeDefs } from '../index'; |
| 5 | +import { Invitation } from '../models/invitation.model'; |
| 6 | +import { User } from '../models/user'; |
| 7 | +// import { checkUserLoggedIn, checkLoggedInOrganization } from '../helpers'; |
| 8 | + |
| 9 | +const mockInvitations = [ |
| 10 | + { |
| 11 | + id: 'invitationId1', |
| 12 | + invitees: [{ email: '[email protected]', role: 'admin' }], |
| 13 | + status: 'pending', |
| 14 | + }, |
| 15 | +]; |
| 16 | + |
| 17 | +const mockOrganization = { name: 'mockOrg' }; |
| 18 | +const mockUser = { id: 'userId1', name: 'Test User' }; |
| 19 | + |
| 20 | +const GET_INVITATIONS_QUERY = gql` |
| 21 | + query GetInvitations($query: String!, $limit: Int, $offset: Int, $orgToken: String!) { |
| 22 | + getInvitations(query: $query, limit: $limit, offset: $offset, orgToken: $orgToken) { |
| 23 | + invitations { |
| 24 | + id |
| 25 | + invitees { |
| 26 | + email |
| 27 | + role |
| 28 | + } |
| 29 | + status |
| 30 | + } |
| 31 | + totalInvitations |
| 32 | + } |
| 33 | + } |
| 34 | +`; |
| 35 | + |
| 36 | +const GET_ALL_INVITATIONS_QUERY = gql` |
| 37 | + query GetAllInvitations($limit: Int, $offset: Int, $orgToken: String!) { |
| 38 | + getAllInvitations(limit: $limit, offset: $offset, orgToken: $orgToken) { |
| 39 | + invitations { |
| 40 | + id |
| 41 | + invitees { |
| 42 | + email |
| 43 | + } |
| 44 | + status |
| 45 | + } |
| 46 | + totalInvitations |
| 47 | + } |
| 48 | + } |
| 49 | +`; |
| 50 | + |
| 51 | +const FILTER_INVITATIONS_QUERY = gql` |
| 52 | + query FilterInvitations( |
| 53 | + $limit: Int |
| 54 | + $offset: Int |
| 55 | + $role: String |
| 56 | + $status: String |
| 57 | + $orgToken: String! |
| 58 | + ) { |
| 59 | + filterInvitations( |
| 60 | + limit: $limit |
| 61 | + offset: $offset |
| 62 | + role: $role |
| 63 | + status: $status |
| 64 | + orgToken: $orgToken |
| 65 | + ) { |
| 66 | + invitations { |
| 67 | + id |
| 68 | + invitees { |
| 69 | + email |
| 70 | + role |
| 71 | + } |
| 72 | + status |
| 73 | + } |
| 74 | + totalInvitations |
| 75 | + } |
| 76 | + } |
| 77 | +`; |
| 78 | + |
| 79 | +describe('TableViewInvitationResolver', () => { |
| 80 | + let testServer: ApolloServer; |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + testServer = new ApolloServer({ |
| 84 | + typeDefs, |
| 85 | + resolvers, |
| 86 | + }); |
| 87 | + |
| 88 | + // Invitation.find = async () => mockInvitations; |
| 89 | + // checkLoggedInOrganization = async () => mockOrganization; |
| 90 | + // checkUserLoggedIn = async () => ({ userId: mockUser.id }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should fetch invitations with a search query', async () => { |
| 94 | + const result = await testServer.executeOperation({ |
| 95 | + query: GET_INVITATIONS_QUERY, |
| 96 | + variables: { |
| 97 | + query: 'test', |
| 98 | + limit: 5, |
| 99 | + offset: 0, |
| 100 | + orgToken: 'someOrgToken', |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(result.body.kind).to.equal('single'); |
| 105 | + // const invitations = result.body.singleResult.data.getInvitations.invitations; |
| 106 | + // expect(invitations).to.be.an('array'); |
| 107 | + // expect(invitations[0].invitees[0].email).to.equal('[email protected]'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should fetch all invitations for an organization', async () => { |
| 111 | + const result = await testServer.executeOperation({ |
| 112 | + query: GET_ALL_INVITATIONS_QUERY, |
| 113 | + variables: { |
| 114 | + limit: 5, |
| 115 | + offset: 0, |
| 116 | + orgToken: 'someOrgToken', |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(result.body.kind).to.equal('single'); |
| 121 | + // const invitations = result.body.singleResult.data.getAllInvitations.invitations; |
| 122 | + // expect(invitations).to.be.an('array'); |
| 123 | + // expect(invitations[0].invitees[0].email).to.equal('[email protected]'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should filter invitations by role and status', async () => { |
| 127 | + const result = await testServer.executeOperation({ |
| 128 | + query: FILTER_INVITATIONS_QUERY, |
| 129 | + variables: { |
| 130 | + role: 'admin', |
| 131 | + status: 'pending', |
| 132 | + orgToken: 'someOrgToken', |
| 133 | + }, |
| 134 | + }); |
| 135 | + |
| 136 | + expect(result.body.kind).to.equal('single'); |
| 137 | + // const invitations = result.body.singleResult.data.filterInvitations.invitations; |
| 138 | + // expect(invitations).to.be.an('array'); |
| 139 | + // expect(invitations[0].invitees[0].role).to.equal('admin'); |
| 140 | + // expect(invitations[0].status).to.equal('pending'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should return an error for invalid orgToken when fetching invitations', async () => { |
| 144 | + const result = await testServer.executeOperation({ |
| 145 | + query: GET_INVITATIONS_QUERY, |
| 146 | + variables: { |
| 147 | + query: 'test', |
| 148 | + limit: 5, |
| 149 | + offset: 0, |
| 150 | + orgToken: '', |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + expect(result.body.kind).to.equal('single'); |
| 155 | + // expect(result.body.singleResult.errors).to.exist; |
| 156 | + // expect(result.body.singleResult.errors[0].message).to.equal('No organization token provided'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should return an error when no query is provided for getInvitations', async () => { |
| 160 | + const result = await testServer.executeOperation({ |
| 161 | + query: GET_INVITATIONS_QUERY, |
| 162 | + variables: { |
| 163 | + query: '', |
| 164 | + limit: 5, |
| 165 | + offset: 0, |
| 166 | + orgToken: 'someOrgToken', |
| 167 | + }, |
| 168 | + }); |
| 169 | + |
| 170 | + expect(result.body.kind).to.equal('single'); |
| 171 | + // expect(result.body.singleResult.errors).to.exist; |
| 172 | + // expect(result.body.singleResult.errors[0].message).to.equal('No query provided'); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should return an error when no filter criteria is provided for filterInvitations', async () => { |
| 176 | + const result = await testServer.executeOperation({ |
| 177 | + query: FILTER_INVITATIONS_QUERY, |
| 178 | + variables: { |
| 179 | + role: '', |
| 180 | + status: '', |
| 181 | + orgToken: 'someOrgToken', |
| 182 | + }, |
| 183 | + }); |
| 184 | + |
| 185 | + expect(result.body.kind).to.equal('single'); |
| 186 | + // expect(result.body.singleResult.errors).to.exist; |
| 187 | + // expect(result.body.singleResult.errors[0].message).to.equal('No filter criteria provided'); |
| 188 | + }); |
| 189 | +}); |
0 commit comments