-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from osher-sade/master
test(server): added tests for graphql server
- Loading branch information
Showing
10 changed files
with
574 additions
and
425 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/middlewares/middleware-activation-condition/filter-soft-delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ResponseMiddlewareParams } from '../middleware'; | ||
import { MiddlewareCondition } from './filter-condition'; | ||
|
||
class FilterSoftDelete implements MiddlewareCondition { | ||
shouldBeReturned({ root, context, info }: ResponseMiddlewareParams): boolean { | ||
return !root.deleted; | ||
} | ||
} | ||
|
||
export const SoftDeleteFilter = new FilterSoftDelete(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { ApolloServer, makeExecutableSchema } from 'apollo-server-koa'; | ||
import { applyMiddleware } from 'graphql-middleware'; | ||
import 'reflect-metadata'; | ||
import { LoggerConfig, PolarisServerConfig } from '../../src/common/injectable-interfaces'; | ||
import { PolarisMiddleware } from '../../src/middlewares/polaris-middleware'; | ||
import { createMiddleware } from '../../src/middlewares/polaris-middleware-creator'; | ||
import { SchemaCreator } from '../../src/schema/utils/schema-creator'; | ||
import { PolarisGraphQLServer } from '../../src/server/graphql-server'; | ||
|
||
const apolloServerMock: { [T in keyof ApolloServer]: any } = { | ||
applyMiddleware: jest.fn(), | ||
} as any; | ||
jest.mock('apollo-server-koa', () => ({ | ||
makeExecutableSchema: jest.fn(), | ||
ApolloServer: jest.fn(() => ({ | ||
applyMiddleware: apolloServerMock.applyMiddleware, | ||
})), | ||
})); | ||
jest.mock('graphql-middleware', () => ({ | ||
applyMiddleware: jest.fn(), | ||
})); | ||
jest.mock('../../src/middlewares/polaris-middleware-creator', () => ({ | ||
createMiddleware: jest.fn(), | ||
})); | ||
|
||
describe('graphql-server tests', () => { | ||
const schemaCreatorMock: { [T in keyof SchemaCreator]: any } = { | ||
generateSchema: jest.fn(() => ({ def: 'definition', resolvers: jest.fn() })), | ||
} as any; | ||
const loggerConfigMock: { [T in keyof LoggerConfig]: any } = {} as any; | ||
const polarisServerConfigMock: { [T in keyof PolarisServerConfig]: any } = { | ||
polarisProperties: jest.fn(), | ||
} as any; | ||
const polarisMiddlewareMock: { [T in keyof PolarisMiddleware]: any } = {} as any; | ||
|
||
test('creating new polaris server - with arguments - generate schema have been called', () => { | ||
const server = new PolarisGraphQLServer( | ||
schemaCreatorMock, | ||
loggerConfigMock, | ||
polarisServerConfigMock, | ||
[polarisMiddlewareMock], | ||
); | ||
|
||
expect(schemaCreatorMock.generateSchema).toHaveBeenCalled(); | ||
}); | ||
|
||
test('creating new polaris server - with arguments - make executable schema have been called with generated typeDefs', () => { | ||
const server = new PolarisGraphQLServer( | ||
schemaCreatorMock, | ||
loggerConfigMock, | ||
polarisServerConfigMock, | ||
[polarisMiddlewareMock], | ||
); | ||
|
||
expect(makeExecutableSchema).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
typeDefs: schemaCreatorMock.generateSchema().def, | ||
}), | ||
); | ||
}); | ||
|
||
test('creating new polaris server - with arguments - graphql apply middleware have been called', () => { | ||
const server = new PolarisGraphQLServer( | ||
schemaCreatorMock, | ||
loggerConfigMock, | ||
polarisServerConfigMock, | ||
[polarisMiddlewareMock], | ||
); | ||
|
||
expect(applyMiddleware).toHaveBeenCalled(); | ||
}); | ||
|
||
test('creating new polaris server - with arguments - create middleware have been called number of times as middlewares provided', () => { | ||
const middlewares = [polarisMiddlewareMock, polarisMiddlewareMock, polarisMiddlewareMock]; | ||
const server = new PolarisGraphQLServer( | ||
schemaCreatorMock, | ||
loggerConfigMock, | ||
polarisServerConfigMock, | ||
middlewares, | ||
); | ||
|
||
expect(createMiddleware).toHaveBeenCalledTimes(middlewares.length); | ||
}); | ||
|
||
test('creating new polaris server - with arguments - apollo server constructor have been called', () => { | ||
const server = new PolarisGraphQLServer( | ||
schemaCreatorMock, | ||
loggerConfigMock, | ||
polarisServerConfigMock, | ||
[polarisMiddlewareMock], | ||
); | ||
|
||
expect(ApolloServer).toHaveBeenCalled(); | ||
}); | ||
|
||
test('creating new polaris server - with arguments - apollo server apply middleware have been called with custom path', () => { | ||
const polarisServerConfigMockWithEndpoint: { [T in keyof PolarisServerConfig]: any } = { | ||
polarisProperties: { endpoint: 'test' }, | ||
} as any; | ||
|
||
const server = new PolarisGraphQLServer( | ||
schemaCreatorMock, | ||
loggerConfigMock, | ||
polarisServerConfigMockWithEndpoint, | ||
[polarisMiddlewareMock], | ||
); | ||
expect(apolloServerMock.applyMiddleware).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
path: polarisServerConfigMockWithEndpoint.polarisProperties.endpoint, | ||
}), | ||
); | ||
}); | ||
}); |