Skip to content

Commit

Permalink
feat: an mock test case
Browse files Browse the repository at this point in the history
  • Loading branch information
HuberTRoy committed Aug 11, 2023
1 parent a23a759 commit f587e64
Show file tree
Hide file tree
Showing 4 changed files with 675 additions and 382 deletions.
5 changes: 5 additions & 0 deletions __mocks__/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2020-2023 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: Apache-2.0

import mockAxios from 'jest-mock-axios';
export default mockAxios;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
"graphql-language-service-server": "^2.8.9",
"husky": "^8.0.1",
"ipfs-http-client": "^53.0.1",
"jest": "^28.1.0",
"jest": "^29.6.2",
"jest-mock-axios": "^4.7.2",
"jwt-decode": "^3.1.2",
"lint-staged": "^13.0.0",
"prettier": "^2.7.1",
"react": "^18.1.0",
"ts-jest": "^28.0.4",
"ts-jest": "^29.1.1",
"ts-node": "^10.8.1"
},
"workspaces": [
Expand Down
66 changes: 64 additions & 2 deletions test/authLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

/* eslint-disable @typescript-eslint/ban-ts-comment */
import dotenv from 'dotenv';
import jwt_decode from 'jwt-decode';

Check warning on line 6 in test/authLink.test.ts

View workflow job for this annotation

GitHub Actions / pr

'jwt_decode' is defined but never used
import axios from 'axios';

import {
ApolloClient,
ApolloLink,
Expand All @@ -21,6 +24,8 @@ import { Logger } from '../packages/apollo-links/src/utils/logger';

dotenv.config();

const mockAxios = axios as jest.Mocked<typeof axios>;

const mockLogger: Logger = {
debug: jest.fn(console.log),
error: jest.fn(console.log),
Expand Down Expand Up @@ -105,6 +110,8 @@ const metadataQuery = gql`
`;

let token = '';
const fakeToken =
'eyJhbCI6IkhTMjU2IiwiYWxnIjoiSFMyNTYifQ.eyJleHAiOiIyMDk5LTA5LTA5In0.kau0kzybKIrHqVzTP8QERsD6nWlnsIjyrqqkEK5iyIA';

describe('auth link', () => {
let client: ApolloClient<unknown>;
Expand All @@ -116,12 +123,39 @@ describe('auth link', () => {
const customFetch = (uri: string, options: any) => {
if (options.headers.authorization) {
token = options.headers.authorization;
expect(token).toContain('Bearer');
}

if (token === `Bearer ${fakeToken}`) {
return Promise.resolve({
json: () =>
Promise.resolve({
data: {
_metadata: {
indexerHealthy: true,
indexerNodeVersion: '00.00',
},
},
}),
text: () =>
Promise.resolve(
JSON.stringify({
data: {
_metadata: {
indexerHealthy: true,
indexerNodeVersion: '00.00',
},
},
})
),
});
}

return fetch(uri, options);
};
client = new ApolloClient({
cache: new InMemoryCache({ resultCaching: true }),
// @ts-ignore
link: from([authLink, new HttpLink({ uri, fetch: customFetch })]),
});
});
Expand All @@ -148,10 +182,38 @@ describe('auth link', () => {
}
};

const queryTestWithMock = async () => {
mockAxios.post.mockImplementation((url, data) => {
if (url.includes('/token')) {
expect(data).toHaveProperty('indexer', options.indexer);
expect(data).toHaveProperty('agreement', options.agreement);
expect(data).toHaveProperty('chain_id', options.chainId);
expect(data).toHaveProperty('consumer', options.consumer);
expect(data).toHaveProperty('deployment_id', options.deploymentId);
// TODO: verify this actually sign with consumer
expect(data).toHaveProperty('signature');
expect(data).toHaveProperty('timestamp');

return Promise.resolve({
data: {
token: fakeToken,
},
});
}

return Promise.resolve();
});

const result = await client.query({ query: metadataQuery, fetchPolicy: 'no-cache' });
expect(result.data._metadata).toBeTruthy();
};

it('can query token with mock', queryTestWithMock);
// the second test case for test query with a not expired token. token set on beforeAll.
it('can query token with mock and exist token', queryTestWithMock);

// the first test case for test fetch token.
it('can query with auth link', queryTest);
// the second test case for test query with a not expired token. token set on beforeAll.
it('can query with cache tokened auth link', queryTest);
});

describe('auth link with auth center', () => {
Expand Down
Loading

0 comments on commit f587e64

Please sign in to comment.