Commit 640ddc75 authored by Miguel Rincon's avatar Miguel Rincon

Move resolvers to a separate file

This change moves the Graph/Axios resolvers to a separate file
so they can be tested in isolation, additonally new mock data is added
to test them.
parent 3231c413
import axios from '~/lib/utils/axios_utils';
const resolvers = {
Mutation: {
lintCI: (_, { endpoint, content, dry_run }) => {
return axios.post(endpoint, { content, dry_run }).then(({ data }) => ({
valid: data.valid,
errors: data.errors,
warnings: data.warnings,
jobs: data.jobs.map(job => {
const only = job.only ? { refs: job.only.refs, __typename: 'CiLintJobOnlyPolicy' } : null;
return {
name: job.name,
stage: job.stage,
beforeScript: job.before_script,
script: job.script,
afterScript: job.after_script,
tagList: job.tag_list,
environment: job.environment,
when: job.when,
allowFailure: job.allow_failure,
only,
except: job.except,
__typename: 'CiLintJob',
};
}),
__typename: 'CiLintContent',
}));
},
},
};
export default resolvers;
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import axios from '~/lib/utils/axios_utils';
import createDefaultClient from '~/lib/graphql';
import CiLint from './components/ci_lint.vue';
import resolvers from './graphql/resolvers';
Vue.use(VueApollo);
const resolvers = {
Mutation: {
lintCI: (_, { endpoint, content, dry_run }) => {
return axios.post(endpoint, { content, dry_run }).then(({ data }) => ({
valid: data.valid,
errors: data.errors,
warnings: data.warnings,
jobs: data.jobs.map(job => {
const only = job.only ? { refs: job.only.refs, __typename: 'CiLintJobOnlyPolicy' } : null;
return {
name: job.name,
stage: job.stage,
beforeScript: job.before_script,
script: job.script,
afterScript: job.after_script,
tagList: job.tag_list,
environment: job.environment,
when: job.when,
allowFailure: job.allow_failure,
only,
except: job.except,
__typename: 'CiLintJob',
};
}),
__typename: 'CiLintContent',
}));
},
},
};
const apolloProvider = new VueApollo({
defaultClient: createDefaultClient(resolvers),
});
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`~/ci_lint/graphql/resolvers Mutation lintCI resolves lint data with type names 1`] = `
Object {
"__typename": "CiLintContent",
"errors": Array [],
"jobs": Array [
Object {
"__typename": "CiLintJob",
"afterScript": Array [
"echo 'after script 1",
],
"allowFailure": false,
"beforeScript": Array [
"echo 'before script 1'",
],
"environment": "prd",
"except": Object {
"refs": Array [
"master@gitlab-org/gitlab",
"/^release/.*$/@gitlab-org/gitlab",
],
},
"name": "job_1",
"only": null,
"script": Array [
"echo 'script 1'",
],
"stage": "test",
"tagList": Array [
"tag 1",
],
"when": "on_success",
},
Object {
"__typename": "CiLintJob",
"afterScript": Array [
"echo 'after script 2",
],
"allowFailure": true,
"beforeScript": Array [
"echo 'before script 2'",
],
"environment": "stg",
"except": Object {
"refs": Array [
"master@gitlab-org/gitlab",
"/^release/.*$/@gitlab-org/gitlab",
],
},
"name": "job_2",
"only": Object {
"__typename": "CiLintJobOnlyPolicy",
"refs": Array [
"web",
"chat",
"pushes",
],
},
"script": Array [
"echo 'script 2'",
],
"stage": "test",
"tagList": Array [
"tag 2",
],
"when": "on_success",
},
],
"valid": true,
"warnings": Array [],
}
`;
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import httpStatus from '~/lib/utils/http_status';
import resolvers from '~/ci_lint/graphql/resolvers';
import { mockLintResponse } from '../mock_data';
describe('~/ci_lint/graphql/resolvers', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('Mutation', () => {
describe('lintCI', () => {
const endpoint = '/ci/lint';
beforeEach(() => {
mock.onPost(endpoint).reply(httpStatus.OK, mockLintResponse);
});
it('resolves lint data with type names', async () => {
const result = resolvers.Mutation.lintCI(null, {
endpoint,
content: 'content',
dry_run: true,
});
await expect(result).resolves.toMatchSnapshot();
});
});
});
});
export const mockLintResponse = {
valid: true,
errors: [],
warnings: [],
jobs: [
{
name: 'job_1',
stage: 'test',
before_script: ["echo 'before script 1'"],
script: ["echo 'script 1'"],
after_script: ["echo 'after script 1"],
tag_list: ['tag 1'],
environment: 'prd',
when: 'on_success',
allow_failure: false,
only: null,
except: { refs: ['master@gitlab-org/gitlab', '/^release/.*$/@gitlab-org/gitlab'] },
},
{
name: 'job_2',
stage: 'test',
before_script: ["echo 'before script 2'"],
script: ["echo 'script 2'"],
after_script: ["echo 'after script 2"],
tag_list: ['tag 2'],
environment: 'stg',
when: 'on_success',
allow_failure: true,
only: { refs: ['web', 'chat', 'pushes'] },
except: { refs: ['master@gitlab-org/gitlab', '/^release/.*$/@gitlab-org/gitlab'] },
},
],
};
export const mockJobs = [
{
name: 'job_1',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment