Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"graphql-tag": "2.12.6",
"jest": "^29.4.0",
"jest-environment-jsdom": "^29.4.0",
"jest-fetch-mock": "^3.0.3",
"jest-sinon": "1.1.0",
"jest-websocket-mock": "^2.4.0",
"mock-socket": "9.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ const SERVER_URL = `ws://localhost:${PORT}`;
export class GraphQLSubscriptionsFixture {
graphqlService = new GraphqlService();
server: WS;
#apiKeyMock: jest.SpyInstance | undefined;

constructor() {
this.openServer();
jest
.spyOn(this.graphqlService as any, 'fetchTemporaryApiKey')
.mockResolvedValue(DUMMY_API_KEY);
jest
.spyOn(this.graphqlService as any, 'getServerUrl')
.mockReturnValue(SERVER_URL);
}

mockApiKeyFetching() {
this.#apiKeyMock = jest
.spyOn(this.graphqlService as any, 'fetchTemporaryApiKey')
.mockResolvedValue(DUMMY_API_KEY);
}

unmockApiKeyFetching() {
this.#apiKeyMock?.mockRestore();
}

triggerSubscription(
query: DocumentNode | string = 'subscription { baba }',
subscriber:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('GraphQL subscriptions', () => {

beforeEach(async () => {
fixture = new GraphQLSubscriptionsFixture();
fixture.mockApiKeyFetching();
});

afterEach(async () => {
Expand Down
24 changes: 24 additions & 0 deletions src/lib/services/graphql/__tests__/graphql-subscriptions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gql from 'graphql-tag';
import fetchMock from 'jest-fetch-mock';
import { WebSocket } from 'mock-socket';
import { Subscriber } from 'rxjs';
import { ConnectionStatus } from '../../../model/connection-status';
Expand Down Expand Up @@ -26,7 +27,9 @@ describe('GraphQL subscriptions', () => {
let fixture: GraphQLSubscriptionsFixture;

beforeEach(async () => {
fetchMock.enableMocks();
fixture = new GraphQLSubscriptionsFixture();
fixture.mockApiKeyFetching();
});

afterEach(async () => {
Expand Down Expand Up @@ -372,6 +375,27 @@ describe('GraphQL subscriptions', () => {

subscription.unsubscribe();
});

describe('API Key', () => {
it('fetches temporary API key', () => {
fixture.graphqlService.setKey('initialkey');
fixture.unmockApiKeyFetching();

fetchMock.mockResponseOnce(JSON.stringify({ key: '12345' }));
fixture.triggerSubscription();

expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(
'https://api.qminder.com/graphql/connection-key',
{
headers: { 'X-Qminder-REST-API-Key': 'initialkey' },
method: 'POST',
mode: 'cors',
},
);
});
});

function useFakeSetInterval() {
jest.useFakeTimers({
doNotFake: [
Expand Down
27 changes: 27 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3850,6 +3850,15 @@ __metadata:
languageName: node
linkType: hard

"cross-fetch@npm:^3.0.4":
version: 3.1.8
resolution: "cross-fetch@npm:3.1.8"
dependencies:
node-fetch: ^2.6.12
checksum: 78f993fa099eaaa041122ab037fe9503ecbbcb9daef234d1d2e0b9230a983f64d645d088c464e21a247b825a08dc444a6e7064adfa93536d3a9454b4745b3632
languageName: node
linkType: hard

"cross-fetch@npm:^4.0.0":
version: 4.0.0
resolution: "cross-fetch@npm:4.0.0"
Expand Down Expand Up @@ -6118,6 +6127,16 @@ __metadata:
languageName: node
linkType: hard

"jest-fetch-mock@npm:^3.0.3":
version: 3.0.3
resolution: "jest-fetch-mock@npm:3.0.3"
dependencies:
cross-fetch: ^3.0.4
promise-polyfill: ^8.1.3
checksum: fb052f7e0ef1c8192a9c15efdd1b18d281ab68fc6b1648b30bff8880fe24418bdf12190ea79b1996932dc15417c3c01f5b2d77ef7104a7e7943e7cbe8d61071d
languageName: node
linkType: hard

"jest-get-type@npm:^29.2.0":
version: 29.2.0
resolution: "jest-get-type@npm:29.2.0"
Expand Down Expand Up @@ -7672,6 +7691,13 @@ __metadata:
languageName: node
linkType: hard

"promise-polyfill@npm:^8.1.3":
version: 8.3.0
resolution: "promise-polyfill@npm:8.3.0"
checksum: 206373802076c77def0805758d0a8ece64120dfa6603f092404a1004211f8f2f67f33cadbc35953fc2a8ed0b0d38c774e88bdf01e20ce7a920723a60df84b7a5
languageName: node
linkType: hard

"promise-retry@npm:^2.0.1":
version: 2.0.1
resolution: "promise-retry@npm:2.0.1"
Expand Down Expand Up @@ -7743,6 +7769,7 @@ __metadata:
isomorphic-ws: ^5.0.0
jest: ^29.4.0
jest-environment-jsdom: ^29.4.0
jest-fetch-mock: ^3.0.3
jest-sinon: 1.1.0
jest-websocket-mock: ^2.4.0
mock-socket: 9.2.1
Expand Down
Loading