Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 9831bc0

Browse files
authored
Merge pull request #271 from sanjam-deriv/testfix
fix: fix broken test case
2 parents 31a2e83 + cc749ec commit 9831bc0

File tree

1 file changed

+125
-133
lines changed

1 file changed

+125
-133
lines changed
Lines changed: 125 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,77 @@
1-
import React from "react";
2-
import "@testing-library/jest-dom";
3-
import userEvent from "@testing-library/user-event";
4-
import { renderHook } from "@testing-library/react-hooks";
5-
import { act } from "react-dom/test-utils";
6-
import useDynamicImportJSON from "..";
7-
import { cleanup, render, screen } from "@testing-library/react";
8-
9-
jest.mock("@docusaurus/router", () => ({
1+
import React from 'react';
2+
import '@testing-library/jest-dom';
3+
import userEvent from '@testing-library/user-event';
4+
import { renderHook } from '@testing-library/react-hooks';
5+
import { act } from 'react-dom/test-utils';
6+
import useDynamicImportJSON from '..';
7+
import { cleanup, render, screen } from '@testing-library/react';
8+
9+
jest.mock('@docusaurus/router', () => ({
1010
useLocation: () => ({
11-
pathname: "/api-explorer#active_symbols",
12-
hash: "#active_symbols",
11+
pathname: '/api-explorer#active_symbols',
12+
hash: '#active_symbols',
1313
}),
1414
useHistory: () => ({
1515
push: jest.fn(),
1616
}),
1717
}));
1818

19-
jest.mock("@site/src/hooks/useAuthContext");
19+
jest.mock('@site/src/hooks/useAuthContext');
2020

21-
describe("useDynamicImportJSON", () => {
21+
describe('useDynamicImportJSON', () => {
2222
const { result } = renderHook(() => useDynamicImportJSON());
2323

2424
afterEach(() => {
2525
jest.clearAllMocks();
2626
cleanup();
2727
});
2828

29-
it("should populate text data with the correct values", () => {
29+
it('should populate text data with the correct values', () => {
3030
act(() => {
3131
expect(result.current.text_data).toEqual({
32-
request:
33-
'{\n "active_symbols": "brief",\n "product_type": "basic"\n}',
34-
selected_value: "Active Symbols",
35-
name: "active_symbols",
32+
request: '{\n "active_symbols": "brief",\n "product_type": "basic"\n}',
33+
selected_value: 'Active Symbols',
34+
name: 'active_symbols',
3635
});
3736
});
3837
});
3938

40-
it("should be able to call handleTextAreaInput when typing in a textarea", async () => {
41-
const spyHandleInputFunction = jest.spyOn(
42-
result.current,
43-
"handleTextAreaInput"
44-
);
39+
it('should be able to call handleTextAreaInput when typing in a textarea', async () => {
40+
const spyHandleInputFunction = jest.spyOn(result.current, 'handleTextAreaInput');
4541

46-
render(
47-
<textarea
48-
placeholder="testtextarea"
49-
onChange={result.current.handleTextAreaInput}
50-
/>
51-
);
42+
render(<textarea placeholder='testtextarea' onChange={result.current.handleTextAreaInput} />);
5243

53-
const textarea = screen.getByPlaceholderText("testtextarea");
44+
const textarea = screen.getByPlaceholderText('testtextarea');
5445
expect(textarea).toBeVisible();
5546

56-
await userEvent.type(textarea, "test123");
47+
await userEvent.type(textarea, 'test123');
5748
expect(spyHandleInputFunction).toHaveBeenCalled();
5849
});
5950

60-
it("should have the correct hash value in the URL on selection of an api call", () => {
61-
const location = require("@docusaurus/router").useLocation();
51+
it('should have the correct hash value in the URL on selection of an api call', () => {
52+
const location = require('@docusaurus/router').useLocation();
6253
const url = location.hash;
63-
expect(url).toMatch("active_symbols");
54+
expect(url).toMatch('active_symbols');
6455
});
6556

66-
it("should check for change in hash value and update text data accordingly", async () => {
67-
jest.mock("@site/src/utils/playground_requests", () => ({
57+
it('should check for change in hash value and update text data accordingly', async () => {
58+
jest.mock('@site/src/utils/playground_requests', () => ({
6859
playground_requests: [
6960
{
70-
name: "active_symbols",
71-
title: "Active Symbols",
61+
name: 'active_symbols',
62+
title: 'Active Symbols',
7263
body: {
73-
active_symbols: "brief",
74-
product_type: "basic",
64+
active_symbols: 'brief',
65+
product_type: 'basic',
7566
},
7667
},
7768
],
7869
}));
7970

80-
jest.mock("@docusaurus/router", () => ({
71+
jest.mock('@docusaurus/router', () => ({
8172
useLocation: () => ({
82-
pathname: "/api-explorer#active_symbols",
83-
hash: "#active_symbol",
73+
pathname: '/api-explorer#active_symbols',
74+
hash: '#active_symbol',
8475
}),
8576
useHistory: () => ({
8677
push: jest.fn(),
@@ -89,173 +80,174 @@ describe("useDynamicImportJSON", () => {
8980

9081
const mockEvent = {
9182
currentTarget: {
92-
value: "active_symbols",
83+
value: 'active_symbols',
9384
},
9485
preventDefault: jest.fn(),
9586
};
9687

97-
const spyHandleSelectChange = jest.spyOn(
98-
result.current,
99-
"handleSelectChange"
100-
);
88+
const spyHandleSelectChange = jest.spyOn(result.current, 'handleSelectChange');
10189

10290
const mockHandleSelectChange = () =>
103-
result.current.handleSelectChange(mockEvent, "active_symbols");
91+
result.current.handleSelectChange(mockEvent, 'active_symbols');
10492

10593
render(
10694
<div>
107-
<button
108-
className="simulated_option"
109-
onClick={() => mockHandleSelectChange()}
110-
>
95+
<button className='simulated_option' onClick={() => mockHandleSelectChange()}>
11196
Active Symbols
11297
</button>
113-
</div>
98+
</div>,
11499
);
115100

116-
const option = screen.getByRole("button", { name: "Active Symbols" });
101+
const option = screen.getByRole('button', { name: 'Active Symbols' });
117102

118103
await userEvent.click(option);
119104

120105
expect(spyHandleSelectChange).toHaveBeenCalled();
121106

122107
expect(result.current.text_data).toEqual({
123108
request: '{\n "active_symbols": "brief",\n "product_type": "basic"\n}',
124-
selected_value: "Active Symbols",
125-
name: "active_symbols",
109+
selected_value: 'Active Symbols',
110+
name: 'active_symbols',
126111
});
127112
});
128113

129-
it("should have correct text area inputs inside dynamic imports correctly", () => {
114+
it('should have correct text area inputs inside dynamic imports correctly', () => {
130115
act(() => {
131116
result.current.dynamicImportJSON(result.current.text_data.selected_value);
132-
console.log(result.current.request_info);
133117
});
134118
expect(result.current.request_info).toEqual({
135-
$schema: "http://json-schema.org/draft-04/schema#",
119+
$schema: 'http://json-schema.org/draft-04/schema#',
136120
additionalProperties: false,
137121
auth_required: 0,
138122
default: {
139-
$schema: "http://json-schema.org/draft-04/schema#",
123+
$schema: 'http://json-schema.org/draft-04/schema#',
140124
additionalProperties: false,
141125
auth_required: 0,
142126
description:
143-
"Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).",
127+
'Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).',
144128
properties: {
145129
active_symbols: {
146-
description:
147-
"If you use `brief`, only a subset of fields will be returned.",
148-
enum: ["brief", "full"],
149-
type: "string",
130+
description: 'If you use `brief`, only a subset of fields will be returned.',
131+
enum: ['brief', 'full'],
132+
type: 'string',
150133
},
151134
landing_company: {
152-
description: "Deprecated - replaced by landing_company_short.",
135+
description: 'Deprecated - replaced by landing_company_short.',
153136
enum: [
154-
"iom",
155-
"malta",
156-
"maltainvest",
157-
"svg",
158-
"virtual",
159-
"vanuatu",
160-
"champion",
161-
"champion-virtual",
137+
'iom',
138+
'malta',
139+
'maltainvest',
140+
'svg',
141+
'virtual',
142+
'vanuatu',
143+
'champion',
144+
'champion-virtual',
162145
],
163-
type: "string",
146+
type: 'string',
164147
},
165148
landing_company_short: {
166149
description:
167-
"[Optional] If you specify this field, only symbols available for trading by that landing company will be returned. If you are logged in, only symbols available for trading by your landing company will be returned regardless of what you specify in this field.",
150+
'[Optional] If you specify this field, only symbols available for trading by that landing company will be returned. If you are logged in, only symbols available for trading by your landing company will be returned regardless of what you specify in this field.',
168151
enum: [
169-
"iom",
170-
"malta",
171-
"maltainvest",
172-
"svg",
173-
"virtual",
174-
"vanuatu",
175-
"champion",
176-
"champion-virtual",
152+
'iom',
153+
'malta',
154+
'maltainvest',
155+
'svg',
156+
'virtual',
157+
'vanuatu',
158+
'champion',
159+
'champion-virtual',
177160
],
178-
type: "string",
161+
type: 'string',
162+
},
163+
loginid: {
164+
description:
165+
"[Optional] The login id of the user. If left unspecified, it defaults to the initial authorized token's login id.",
166+
pattern: '^[A-Za-z]+[0-9]+$',
167+
type: 'string',
179168
},
180169
passthrough: {
181170
description:
182-
"[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.",
183-
maxSize: 3500,
184-
type: "object",
171+
'[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.',
172+
type: 'object',
185173
},
186174
product_type: {
187175
description:
188-
"[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.",
189-
enum: ["basic"],
190-
type: "string",
176+
'[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.',
177+
enum: ['basic'],
178+
type: 'string',
191179
},
192180
req_id: {
193-
description: "[Optional] Used to map request to response.",
194-
type: "integer",
181+
description: '[Optional] Used to map request to response.',
182+
type: 'integer',
195183
},
196184
},
197-
required: ["active_symbols"],
198-
title: "Active Symbols (request)",
199-
type: "object",
185+
required: ['active_symbols'],
186+
title: 'Active Symbols (request)',
187+
type: 'object',
200188
},
201189
description:
202-
"Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).",
190+
'Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).',
203191
properties: {
204192
active_symbols: {
205-
description:
206-
"If you use `brief`, only a subset of fields will be returned.",
207-
enum: ["brief", "full"],
208-
type: "string",
193+
description: 'If you use `brief`, only a subset of fields will be returned.',
194+
enum: ['brief', 'full'],
195+
type: 'string',
209196
},
210197
landing_company: {
211-
description: "Deprecated - replaced by landing_company_short.",
198+
description: 'Deprecated - replaced by landing_company_short.',
212199
enum: [
213-
"iom",
214-
"malta",
215-
"maltainvest",
216-
"svg",
217-
"virtual",
218-
"vanuatu",
219-
"champion",
220-
"champion-virtual",
200+
'iom',
201+
'malta',
202+
'maltainvest',
203+
'svg',
204+
'virtual',
205+
'vanuatu',
206+
'champion',
207+
'champion-virtual',
221208
],
222-
type: "string",
209+
type: 'string',
223210
},
224211
landing_company_short: {
225212
description:
226-
"[Optional] If you specify this field, only symbols available for trading by that landing company will be returned. If you are logged in, only symbols available for trading by your landing company will be returned regardless of what you specify in this field.",
213+
'[Optional] If you specify this field, only symbols available for trading by that landing company will be returned. If you are logged in, only symbols available for trading by your landing company will be returned regardless of what you specify in this field.',
227214
enum: [
228-
"iom",
229-
"malta",
230-
"maltainvest",
231-
"svg",
232-
"virtual",
233-
"vanuatu",
234-
"champion",
235-
"champion-virtual",
215+
'iom',
216+
'malta',
217+
'maltainvest',
218+
'svg',
219+
'virtual',
220+
'vanuatu',
221+
'champion',
222+
'champion-virtual',
236223
],
237-
type: "string",
224+
type: 'string',
225+
},
226+
loginid: {
227+
description:
228+
"[Optional] The login id of the user. If left unspecified, it defaults to the initial authorized token's login id.",
229+
pattern: '^[A-Za-z]+[0-9]+$',
230+
type: 'string',
238231
},
239232
passthrough: {
240233
description:
241-
"[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.",
242-
maxSize: 3500,
243-
type: "object",
234+
'[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.',
235+
type: 'object',
244236
},
245237
product_type: {
246238
description:
247-
"[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.",
248-
enum: ["basic"],
249-
type: "string",
239+
'[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.',
240+
enum: ['basic'],
241+
type: 'string',
250242
},
251243
req_id: {
252-
description: "[Optional] Used to map request to response.",
253-
type: "integer",
244+
description: '[Optional] Used to map request to response.',
245+
type: 'integer',
254246
},
255247
},
256-
required: ["active_symbols"],
257-
title: "Active Symbols (request)",
258-
type: "object",
248+
required: ['active_symbols'],
249+
title: 'Active Symbols (request)',
250+
type: 'object',
259251
});
260252
});
261253
});

0 commit comments

Comments
 (0)