From 2696a7eb5b762fd9149ebf68ef433259e230ee0a Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 7 Oct 2021 00:11:23 +0200 Subject: [PATCH] feat: Setup Unit Tests for Resolvers --- backend/package.json | 1 + .../resolver/CommunityResolver.test.ts | 39 +++++++++++++++++++ .../src/graphql/resolver/CommunityResolver.ts | 7 +--- backend/src/test-server.ts | 29 ++++++++++++++ backend/yarn.lock | 7 ++++ 5 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 backend/src/graphql/resolver/CommunityResolver.test.ts create mode 100644 backend/src/test-server.ts diff --git a/backend/package.json b/backend/package.json index 4719595bb..01952bf69 100644 --- a/backend/package.json +++ b/backend/package.json @@ -18,6 +18,7 @@ "dependencies": { "@types/jest": "^27.0.2", "apollo-server-express": "^2.25.2", + "apollo-server-testing": "^2.25.2", "axios": "^0.21.1", "class-validator": "^0.13.1", "cors": "^2.8.5", diff --git a/backend/src/graphql/resolver/CommunityResolver.test.ts b/backend/src/graphql/resolver/CommunityResolver.test.ts new file mode 100644 index 000000000..fa295c82f --- /dev/null +++ b/backend/src/graphql/resolver/CommunityResolver.test.ts @@ -0,0 +1,39 @@ +import { createTestClient } from 'apollo-server-testing' +import createTestServer from '../../test-server' + +let query: any + +beforeAll(async () => { + const apollo = createTestServer() + + query = createTestClient(await apollo).query +}) + +describe('CommunityResolver', () => { + + const getCommunityInfoQuery = ` + query { + getCommunityInfo() { + name + description + url + registerUrl + } + } + ` + + describe('getCommunityInfo', () => { + it('returns the default values', async () => { + await expect(query({ query: getCommunityInfoQuery })).resolves.toMatchObject({ + data: { + getCommunityInfo: { + name: 'Gradido Entwicklung', + description: 'Die lokale Entwicklungsumgebung von Gradido.', + url: 'http://localhost/vue/', + registerUrl: 'http://localhost/vue/register', + }, + }, + }) + }) + }) +}) diff --git a/backend/src/graphql/resolver/CommunityResolver.ts b/backend/src/graphql/resolver/CommunityResolver.ts index 563c73d24..836c61b66 100644 --- a/backend/src/graphql/resolver/CommunityResolver.ts +++ b/backend/src/graphql/resolver/CommunityResolver.ts @@ -19,9 +19,7 @@ export class CommunityResolver { @Query(() => [Community]) async communities(): Promise { - const communities: Community[] = [] - - communities.push( + return [ new Community({ id: 1, name: 'Gradido Entwicklung', @@ -43,7 +41,6 @@ export class CommunityResolver { url: 'https://gradido.net', registerUrl: 'https://gdd1.gradido.com/vue/register-community', }), - ) - return communities + ] } } diff --git a/backend/src/test-server.ts b/backend/src/test-server.ts new file mode 100644 index 000000000..ab6edd0df --- /dev/null +++ b/backend/src/test-server.ts @@ -0,0 +1,29 @@ +import { ApolloServer } from 'apollo-server-express' +import express from 'express' +import cors from './server/cors' +import context from './server/context' +import plugins from './server/plugins' +import CONFIG from './config' + +// graphql +import schema from './graphql/schema' + +const createTestServer = async () => { + // Express Server + const server = express() + + // cors + server.use(cors) + + // Apollo Server + const apollo = new ApolloServer({ + schema: await schema(), + playground: CONFIG.GRAPHIQL, + context, + plugins, + }) + apollo.applyMiddleware({ app: server }) + return apollo +} + +export default createTestServer diff --git a/backend/yarn.lock b/backend/yarn.lock index 714823fe9..7b3310a02 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -1328,6 +1328,13 @@ apollo-server-plugin-base@^0.13.0: dependencies: apollo-server-types "^0.9.0" +apollo-server-testing@^2.25.2: + version "2.25.2" + resolved "https://registry.yarnpkg.com/apollo-server-testing/-/apollo-server-testing-2.25.2.tgz#0043e98b1a03720352e94b409215fb4782ae2e50" + integrity sha512-HjQV9wPbi/ZqpRbyyhNwCbaDnfjDM0hTRec5TOoOjurEZ/vh4hTPHwGkDZx3kbcWowhGxe2qoHM6KANSB/SxuA== + dependencies: + apollo-server-core "^2.25.2" + apollo-server-types@^0.9.0: version "0.9.0" resolved "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-0.9.0.tgz"