Skip to content

Commit

Permalink
test: Add tests for resolvers (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinc authored Feb 22, 2021
1 parent 8a3ddde commit 905d082
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 86 deletions.
13 changes: 13 additions & 0 deletions src/graphql/resolvers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const resolvers = require('./resolvers');

describe('resolvers', () => {
it('should contain all resolvers', () => {
expect(resolvers).toHaveProperty('Commit');
expect(resolvers).toHaveProperty('Config');
expect(resolvers).toHaveProperty('GitObject');
expect(resolvers).toHaveProperty('Promotion');
expect(resolvers).toHaveProperty('Query');
expect(resolvers).toHaveProperty('Status');
expect(resolvers).toHaveProperty('Ticket');
});
});
23 changes: 12 additions & 11 deletions src/graphql/resolvers/commit.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const { findDataSource } = require('../utils');

module.exports = {
message(parent) {
return parent.commit.message;
message(commit) {
return commit.commit.message;
},

commitType(parent) {
if (parent.parents.length > 2) {
commitType(commit) {
if (commit.parents.length > 2) {
return 'octopus';
}

if (parent.parents.length > 1) {
if (commit.parents.length > 1) {
return 'merge';
}

return 'single';
},

async promotions(commit, args, { config, dataSources }) {
async promotions(commit, args, { config, dataSources } = {}) {
const dataSource = findDataSource({
name: config.promotions.connection,
dataSources,
Expand All @@ -36,8 +36,11 @@ module.exports = {
return projects1;
},

async tickets(commit, args, { org, project, config, dataSources }) {
async tickets(commit, args, { org, project, config, dataSources } = {}) {
const { sha } = commit;
if (!sha) {
return null;
}

const dataSource = findDataSource({
name: config.tickets.connection,
Expand All @@ -57,9 +60,7 @@ module.exports = {
return results;
},

commits(parent) {
// console.log(Object.keys(parent));
// console.log(Object.keys(parent.parents));
return parent.parents;
commits(commit) {
return commit.parents;
},
};
57 changes: 57 additions & 0 deletions src/graphql/resolvers/commit.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
jest.mock('config');

const commitResolver = require('./commit');

describe('resolvers', () => {
describe('Commit', () => {
describe('message', () => {
it('should return correctly', () => {
expect(
commitResolver.message({ commit: { message: 'lorem' } })
).toEqual('lorem');
});
});

describe('commitType', () => {
it('should return single for one commit', () => {
expect(
commitResolver.commitType({
parents: ['sha1'],
})
).toEqual('single');
});

it('should return merge for two commits', () => {
expect(
commitResolver.commitType({
parents: ['sha1', 'sha2'],
})
).toEqual('merge');
});

it('should return octopus for three commits', () => {
expect(
commitResolver.commitType({
parents: ['sha1', 'sha2', 'sha3'],
})
).toEqual('octopus');
});

it('should return single for no commit', () => {
expect(
commitResolver.commitType({
parents: [],
})
).toEqual('single');
});
});

describe('tickets', () => {
it('should null with a missing commit sha', async () => {
expect(
await commitResolver.tickets({ commit: { message: 'lorem' } })
).toEqual(null);
});
});
});
});
4 changes: 2 additions & 2 deletions src/graphql/resolvers/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
promotions(parent) {
return parent.promotions.jobs;
promotions(config) {
return config.promotions.jobs;
},
};
30 changes: 30 additions & 0 deletions src/graphql/resolvers/config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
jest.mock('config');

const configResolver = require('./config');

describe('resolvers', () => {
describe('Config', () => {
describe('promotions', () => {
it('should jobs', () => {
expect(
configResolver.promotions({
promotions: {
connection: 'circleci-public',
jobs: [
'build',
'deploy_integration',
'deploy_staging',
'deploy_prod',
],
},
})
).toEqual([
'build',
'deploy_integration',
'deploy_staging',
'deploy_prod',
]);
});
});
});
});
1 change: 0 additions & 1 deletion src/graphql/resolvers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ module.exports = {
}

context.config = contextConfig;

/* eslint-enable no-param-reassign */

return {
Expand Down
110 changes: 52 additions & 58 deletions src/graphql/resolvers/query.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
jest.mock('config');

const { ApolloError } = require('apollo-server');

const appConfig = require('config');
const queryResolver = require('./query');

describe('resolvers', () => {
let configData;
beforeEach(() => {
configData = {
projects: [
{
org: 'merlinc',
project: 'release-status-testing',
type: 'web',
},
],
};
appConfig.get = jest.fn().mockReturnValueOnce(configData.projects);
});

describe('Query', () => {
describe('status', () => {
let configData;
beforeEach(() => {
configData = {
projects: [
{
org: 'merlinc',
project: 'release-status-testing',
type: 'web',
},
],
};
appConfig.get = jest.fn().mockReturnValueOnce(configData.projects);
});

it('should return org and project', async () => {
const result = await queryResolver.status(
{},
Expand Down Expand Up @@ -54,69 +56,36 @@ describe('resolvers', () => {

expect(context.config).toEqual(configData.projects[0]);
});
});

describe('list', () => {
let configListData;
beforeEach(() => {
configListData = [
{
org: 'merlinc',
project: 'release-status-testing',
type: 'web',
extends: ['github'],
},
{
org: 'merlinc',
project: 'release-status-graphql',
type: 'web',
extends: ['github'],
},
];
it('should return null if config not found', async () => {
const context = {};
const result = await queryResolver.status(
{},
{ org: 'merlinc', project: 'release-status-nonexistent' },
context
);

appConfig.get = jest.fn().mockReturnValueOnce(configListData);
expect(result).toBeNull();
});
});

describe('list', () => {
it('should return all data', async () => {
const result = await queryResolver.list();

expect(result.length).toBe(2);
});

it('should transform data', async () => {
const result = await queryResolver.list();

expect(result).toEqual([
{
org: 'merlinc',
project: 'release-status-testing',
type: 'web',
},
{
org: 'merlinc',
project: 'release-status-graphql',
type: 'web',
},
]);
});
});

describe('config', () => {
let configItemData;
beforeEach(() => {
configItemData = [
{
org: 'merlinc',
project: 'release-status-testing',
type: 'web',
},
];

appConfig.get = jest.fn().mockReturnValueOnce(configItemData);
});

it('should return org and project', async () => {
const result = await queryResolver.status(
const result = await queryResolver.config(
{},
{ org: 'merlinc', project: 'release-status-testing' },
{}
Expand All @@ -125,6 +94,31 @@ describe('resolvers', () => {
expect(result.org).toEqual('merlinc');
expect(result.project).toEqual('release-status-testing');
});

it('should return an Apollo error if org not found', async () => {
const result = await queryResolver.config(
{},
{ org: 'merlinc-notfound', project: 'release-status-testing' },
{}
);

expect(result).toBeInstanceOf(ApolloError);
expect(result.message).toEqual(
'org: merlinc-notfound / project: release-status-testing not found'
);
});
it('should return an Apollo error if project not found', async () => {
const result = await queryResolver.config(
{},
{ org: 'merlinc', project: 'release-status-nonexistent' },
{}
);

expect(result).toBeInstanceOf(ApolloError);
expect(result.message).toEqual(
'org: merlinc / project: release-status-nonexistent not found'
);
});
});
});
});
1 change: 0 additions & 1 deletion src/graphql/resolvers/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = {
config,
});

// console.log(result);
return result;
},
};
12 changes: 6 additions & 6 deletions src/graphql/resolvers/ticket.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module.exports = {
id(parent) {
return parent.number;
id(ticket) {
return ticket.number;
},

status(parent) {
return parent.state;
status(ticket) {
return ticket.state;
},

title(parent) {
return parent.title;
title(ticket) {
return ticket.title;
},
};
16 changes: 9 additions & 7 deletions src/graphql/resolvers/ticket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ const ticketResolver = require('./ticket');
describe('resolvers', () => {
describe('Ticket', () => {
describe('id', () => {
it('should return correctly', () => {
expect(ticketResolver.id({ number: 100 })).toEqual(100);
it('should return number', () => {
expect(ticketResolver.id({ number: 1234 })).toEqual(1234);
});
});

describe('status', () => {
it('should return correctly', () => {
expect(ticketResolver.status({ state: 'ok' })).toEqual('ok');
it('should return state', () => {
expect(
ticketResolver.status({ number: 1234, state: 'in review' })
).toEqual('in review');
});
});

describe('title', () => {
it('should return correctly', () => {
expect(ticketResolver.title({ title: 'This is a title' })).toEqual(
'This is a title'
it('should return number', () => {
expect(ticketResolver.title({ title: 'Fix issues' })).toEqual(
'Fix issues'
);
});
});
Expand Down

0 comments on commit 905d082

Please sign in to comment.