Skip to content

Commit

Permalink
Merge pull request #54 from babbel/msw
Browse files Browse the repository at this point in the history
Mock using MSW
  • Loading branch information
jtsaito authored Nov 29, 2023
2 parents 0a035de + fda1f86 commit 5bd0bd7
Show file tree
Hide file tree
Showing 6 changed files with 898 additions and 371 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.0.5] - 2023-11-29
- Replace fetch-mock with MSW for mocking requests in tests.

## [1.0.4] - 2023-11-23
- Update action specification to node.js 20 and update actions/github lib.

Expand Down
79 changes: 48 additions & 31 deletions __tests__/apiwrapper.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import { Octokit } from '@octokit/core';
import fetchMock from 'fetch-mock'; // https://github.com/wheresrhys/fetch-mock

import { paginateGraphql } from '@octokit/plugin-paginate-graphql';

import { graphql, HttpResponse } from 'msw'; // https://mswjs.io/docs/getting-started
import { setupServer } from 'msw/node'; // https://mswjs.io/docs/getting-started/integrate/node

import { ApiWrapper } from '../apiwrapper';

const GraphQlOctokit = Octokit.plugin(paginateGraphql);
const octokit = new GraphQlOctokit({ auth: 'fake-token-value' }); // don't use default GITHUB_TOKEN token from env

const apiWrapper = new ApiWrapper({ octokit });

const mockResponse = (name, data) => {
fetchMock.postOnce({
name,
matcher: 'https://api.github.com/graphql',
response: {
status: 200,
body: { data },
},
});
const server = setupServer();

const mock = ({ action, matcher, data }) => {
const actions = {
mutation: graphql.mutation,
query: graphql.query,
};

server.use(
actions[action](matcher, () => HttpResponse.json({ data })),
);
};

describe('ApiWrapper', () => {
beforeAll(() => {
server.listen();
});

afterEach(() => {
server.resetHandlers();
});

afterAll(() => {
fetchMock.reset();
server.close();
});

describe('.fetchAssignedProjects()', () => {
Expand All @@ -44,14 +56,14 @@ describe('ApiWrapper', () => {
},
};

beforeEach(() => {
mock({ action: 'query', matcher: /paginate/, data });
});

const input = {
pullRequestId: 'PVT_0000000000000001',
};

beforeEach(() => { mockResponse('fetchAssignedProjects', data); });

afterEach(() => { fetchMock.reset(); });

test('returns nodes', async () => {
const nodes = await apiWrapper.fetchAssignedProjects(input);
expect(nodes).toEqual(data.node.projectsV2.nodes); // checks deep
Expand Down Expand Up @@ -80,14 +92,15 @@ describe('ApiWrapper', () => {
},
};

beforeEach(() => {
mock({ action: 'query', matcher: /paginate/, data });
});

const input = {
project: { id: 'PVT_000000000000001' },
pullRequestId: 'PR_0000000000000001',
};

beforeEach(() => { mockResponse('fetchItemForPRId', data); });
afterEach(() => { fetchMock.reset(); });

test('returns project v2 item node', async () => {
const node = await apiWrapper.fetchItemForPRId(input);
expect(node).toEqual(data.node.items.nodes[0]); // checks deep
Expand All @@ -101,15 +114,16 @@ describe('ApiWrapper', () => {
},
};

beforeEach(() => {
mock({ action: 'mutation', matcher: /deleteProjectV2Item/, data });
});

const input = {
project: { id: 'PVT_000000000000001' },
item: { id: 'PVTI_00000000000000000000000' },
clientMutationId: 'foo',
};

beforeEach(() => { mockResponse('deleteProjectItem', data); });
afterEach(() => { fetchMock.reset(); });

test('returns id of delted item', async () => {
const id = await apiWrapper.deleteProjectItem(input);
expect(id).toEqual(data.deleteProjectV2Item); // checks deep
Expand Down Expand Up @@ -140,14 +154,15 @@ describe('ApiWrapper', () => {
},
};

beforeAll(() => {
mock({ action: 'query', matcher: /paginate/, data });
});

const input = {
owner: 'acme',
repositoryName: 'example-repository-name',
};

beforeEach(() => { mockResponse('fetchRepositoryAndProjects', data); });
afterEach(() => { fetchMock.reset(); });

test('returns object containing id', async () => {
const repository = await apiWrapper.fetchRepositoryAndProjects(input);
expect({ repository }).toEqual(data); // checks deep
Expand All @@ -163,15 +178,16 @@ describe('ApiWrapper', () => {
},
};

beforeAll(() => {
mock({ action: 'mutation', matcher: /assignPRtoProject/, data });
});

const input = {
pullRequestId: 'PR_0000000000000001',
project: { id: 'PVT_0000000000000001' },
clientMutationId: 'foo',
};

beforeEach(() => { mockResponse('assignPRtoProject', data); });
afterEach(() => { fetchMock.reset(); });

test('returns object containing proect item', async () => {
const item = await apiWrapper.assignPRtoProject(input);
expect({ item }).toEqual(data.addProjectV2ItemById); // checks deep
Expand All @@ -183,6 +199,10 @@ describe('ApiWrapper', () => {
updateProjectV2ItemFieldValue: { projectV2Item: { id: 'PVTI_0000000000000001' } },
};

beforeAll(() => {
mock({ action: 'mutation', matcher: /updateItemFieldValue/, data });
});

const input = {
project: { id: 'PVT_0000000000000001' },
item: { id: 'PVTI_0000000000000001' },
Expand All @@ -191,9 +211,6 @@ describe('ApiWrapper', () => {
clientMutationId: 'foo',
};

beforeEach(() => { mockResponse('updateItemFieldValue', data); });
afterEach(() => { fetchMock.reset(); });

test('returns updated field value item', async () => {
const result = await apiWrapper.updateItemFieldValue(input);
expect(result).toEqual(data); // checks deep
Expand Down
Loading

0 comments on commit 5bd0bd7

Please sign in to comment.