Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Jan 20, 2022
1 parent d48a31c commit a3dadb7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 51 deletions.
21 changes: 15 additions & 6 deletions packages/e2e/__tests__/servers/server-graphql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { AccountsClient } from '@accounts/client';
import { AccountsClientPassword } from '@accounts/client-password';
import { AccountsModule } from '@accounts/graphql-api';
import {
context,
createAccountsCoreModule,
createAccountsPasswordModule,
} from '@accounts/graphql-api';
import { createApplication } from 'graphql-modules';
import { AccountsGraphQLClient } from '@accounts/graphql-client';
import { AccountsPassword } from '@accounts/password';
import { AccountsServer } from '@accounts/server';
Expand Down Expand Up @@ -77,13 +82,17 @@ export class ServerGraphqlTest implements ServerTestInterface {
password: this.accountsPassword,
}
);
const { schema, context } = AccountsModule.forRoot({
accountsServer: this.accountsServer,
});

this.apolloServer = new ApolloServer({
schema,
context,
schema: createApplication({
modules: [
createAccountsCoreModule({ accountsServer: this.accountsServer }),
createAccountsPasswordModule({ accountsPassword: this.accountsPassword }),
],
}).createSchemaForApollo(),
context: ({ req }) => {
return context({ req }, { accountsServer: this.accountsServer });
},
});

const apolloClient = new ApolloClient({
Expand Down
41 changes: 7 additions & 34 deletions packages/graphql-api/__tests__/modules/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { print } from 'graphql';
import { AccountsModule } from '../../../src/modules/accounts/index';
import { createApplication } from 'graphql-modules';
import { createAccountsCoreModule } from '../../../src/modules/accounts/index';
import { mergeTypeDefs } from '@graphql-tools/merge';

const accountsServer = {
getServices: () => ({
Expand All @@ -9,39 +11,10 @@ const accountsServer = {
};

describe('AccountsModule', () => {
describe('withSchemaDefinition', () => {
it('should export typeDefs with schema definition', () => {
const accountsGraphQL = AccountsModule.forRoot({
accountsServer: accountsServer as any,
withSchemaDefinition: true,
});
expect(print(accountsGraphQL.typeDefs)).toMatch(/schema/);
});

it('should export typeDefs without schema definition', () => {
const accountsGraphQL = AccountsModule.forRoot({
accountsServer: accountsServer as any,
withSchemaDefinition: false,
});
expect(print(accountsGraphQL.typeDefs)).not.toMatch(/schema/);
});
});

describe('extendTypeDefs', () => {
it('should extends typeDefs', () => {
const accountsGraphQL = AccountsModule.forRoot({
accountsServer: accountsServer as any,
extendTypeDefs: true,
});
expect(print(accountsGraphQL.typeDefs)).toMatch(/extend/);
});

it('should not extends typeDefs', () => {
const accountsGraphQL = AccountsModule.forRoot({
accountsServer: accountsServer as any,
extendTypeDefs: false,
});
expect(print(accountsGraphQL.typeDefs)).not.toMatch(/extend/);
it('should export typeDefs with schema definition', () => {
const accountsGraphQL = createApplication({
modules: [createAccountsCoreModule({ accountsServer } as any)],
});
expect(print(mergeTypeDefs(accountsGraphQL.typeDefs))).toMatch(/schema/);
});
});
18 changes: 7 additions & 11 deletions packages/graphql-api/__tests__/utils/context-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ describe('context-builder', () => {
const accountsServerMock = {
resumeSession: jest.fn<any, any>(() => user),
};
const injector = {
get: jest.fn(() => ({
accountsServer: accountsServerMock,
})),
};
const authToken = 'authTokenTest';
const ip = '0.0.0.0';
const reqMock = {
Expand All @@ -25,24 +20,25 @@ describe('context-builder', () => {

it('should not resume session is header is empty', async () => {
accountsServerMock.resumeSession.mockRejectedValueOnce(true);
await context('test')!({ req: { headers: {} } } as any, {}, { injector } as any);
expect(injector.get).toHaveBeenCalled();
await context({ req: { headers: {} } } as any, { accountsServer: accountsServerMock } as any);
expect(accountsServerMock.resumeSession).not.toHaveBeenCalled();
});

it('should resume session and inject the user', async () => {
accountsServerMock.resumeSession.mockRejectedValueOnce(true);
const data = await context('test')!({ req: reqMock } as any, {}, { injector } as any);
expect(injector.get).toHaveBeenCalled();
const data = await context(
{ req: reqMock } as any,
{ accountsServer: accountsServerMock } as any
);
expect(accountsServerMock.resumeSession).toHaveBeenCalledWith(authToken);
expect(data.authToken).toBe(authToken);
expect(data.ip).toBe(ip);
});

it('should allow no request in context', async () => {
const data = await context('test')!({} as any, {}, { injector } as any);
const data = await context({} as any, { accountsServer: accountsServerMock } as any);
expect(data.ip).toBe('');
expect(data.userAgent).toBe('');
expect(injector.get).not.toHaveBeenCalled();
expect(accountsServerMock.resumeSession).not.toHaveBeenCalled();
});
});

0 comments on commit a3dadb7

Please sign in to comment.