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

fix: fix broken test case #271

Merged
merged 1 commit into from
Dec 7, 2023
Merged
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
258 changes: 125 additions & 133 deletions src/hooks/useDynamicImportJSON/__tests__/useDynamicImport.test.tsx
Original file line number Diff line number Diff line change
@@ -1,86 +1,77 @@
import React from "react";
import "@testing-library/jest-dom";
import userEvent from "@testing-library/user-event";
import { renderHook } from "@testing-library/react-hooks";
import { act } from "react-dom/test-utils";
import useDynamicImportJSON from "..";
import { cleanup, render, screen } from "@testing-library/react";

jest.mock("@docusaurus/router", () => ({
import React from 'react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
import { renderHook } from '@testing-library/react-hooks';
import { act } from 'react-dom/test-utils';
import useDynamicImportJSON from '..';
import { cleanup, render, screen } from '@testing-library/react';

jest.mock('@docusaurus/router', () => ({
useLocation: () => ({
pathname: "/api-explorer#active_symbols",
hash: "#active_symbols",
pathname: '/api-explorer#active_symbols',
hash: '#active_symbols',
}),
useHistory: () => ({
push: jest.fn(),
}),
}));

jest.mock("@site/src/hooks/useAuthContext");
jest.mock('@site/src/hooks/useAuthContext');

describe("useDynamicImportJSON", () => {
describe('useDynamicImportJSON', () => {
const { result } = renderHook(() => useDynamicImportJSON());

afterEach(() => {
jest.clearAllMocks();
cleanup();
});

it("should populate text data with the correct values", () => {
it('should populate text data with the correct values', () => {
act(() => {
expect(result.current.text_data).toEqual({
request:
'{\n "active_symbols": "brief",\n "product_type": "basic"\n}',
selected_value: "Active Symbols",
name: "active_symbols",
request: '{\n "active_symbols": "brief",\n "product_type": "basic"\n}',
selected_value: 'Active Symbols',
name: 'active_symbols',
});
});
});

it("should be able to call handleTextAreaInput when typing in a textarea", async () => {
const spyHandleInputFunction = jest.spyOn(
result.current,
"handleTextAreaInput"
);
it('should be able to call handleTextAreaInput when typing in a textarea', async () => {
const spyHandleInputFunction = jest.spyOn(result.current, 'handleTextAreaInput');

render(
<textarea
placeholder="testtextarea"
onChange={result.current.handleTextAreaInput}
/>
);
render(<textarea placeholder='testtextarea' onChange={result.current.handleTextAreaInput} />);

const textarea = screen.getByPlaceholderText("testtextarea");
const textarea = screen.getByPlaceholderText('testtextarea');
expect(textarea).toBeVisible();

await userEvent.type(textarea, "test123");
await userEvent.type(textarea, 'test123');
expect(spyHandleInputFunction).toHaveBeenCalled();
});

it("should have the correct hash value in the URL on selection of an api call", () => {
const location = require("@docusaurus/router").useLocation();
it('should have the correct hash value in the URL on selection of an api call', () => {
const location = require('@docusaurus/router').useLocation();
const url = location.hash;
expect(url).toMatch("active_symbols");
expect(url).toMatch('active_symbols');
});

it("should check for change in hash value and update text data accordingly", async () => {
jest.mock("@site/src/utils/playground_requests", () => ({
it('should check for change in hash value and update text data accordingly', async () => {
jest.mock('@site/src/utils/playground_requests', () => ({
playground_requests: [
{
name: "active_symbols",
title: "Active Symbols",
name: 'active_symbols',
title: 'Active Symbols',
body: {
active_symbols: "brief",
product_type: "basic",
active_symbols: 'brief',
product_type: 'basic',
},
},
],
}));

jest.mock("@docusaurus/router", () => ({
jest.mock('@docusaurus/router', () => ({
useLocation: () => ({
pathname: "/api-explorer#active_symbols",
hash: "#active_symbol",
pathname: '/api-explorer#active_symbols',
hash: '#active_symbol',
}),
useHistory: () => ({
push: jest.fn(),
Expand All @@ -89,173 +80,174 @@ describe("useDynamicImportJSON", () => {

const mockEvent = {
currentTarget: {
value: "active_symbols",
value: 'active_symbols',
},
preventDefault: jest.fn(),
};

const spyHandleSelectChange = jest.spyOn(
result.current,
"handleSelectChange"
);
const spyHandleSelectChange = jest.spyOn(result.current, 'handleSelectChange');

const mockHandleSelectChange = () =>
result.current.handleSelectChange(mockEvent, "active_symbols");
result.current.handleSelectChange(mockEvent, 'active_symbols');

render(
<div>
<button
className="simulated_option"
onClick={() => mockHandleSelectChange()}
>
<button className='simulated_option' onClick={() => mockHandleSelectChange()}>
Active Symbols
</button>
</div>
</div>,
);

const option = screen.getByRole("button", { name: "Active Symbols" });
const option = screen.getByRole('button', { name: 'Active Symbols' });

await userEvent.click(option);

expect(spyHandleSelectChange).toHaveBeenCalled();

expect(result.current.text_data).toEqual({
request: '{\n "active_symbols": "brief",\n "product_type": "basic"\n}',
selected_value: "Active Symbols",
name: "active_symbols",
selected_value: 'Active Symbols',
name: 'active_symbols',
});
});

it("should have correct text area inputs inside dynamic imports correctly", () => {
it('should have correct text area inputs inside dynamic imports correctly', () => {
act(() => {
result.current.dynamicImportJSON(result.current.text_data.selected_value);
console.log(result.current.request_info);
});
expect(result.current.request_info).toEqual({
$schema: "http://json-schema.org/draft-04/schema#",
$schema: 'http://json-schema.org/draft-04/schema#',
additionalProperties: false,
auth_required: 0,
default: {
$schema: "http://json-schema.org/draft-04/schema#",
$schema: 'http://json-schema.org/draft-04/schema#',
additionalProperties: false,
auth_required: 0,
description:
"Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).",
'Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).',
properties: {
active_symbols: {
description:
"If you use `brief`, only a subset of fields will be returned.",
enum: ["brief", "full"],
type: "string",
description: 'If you use `brief`, only a subset of fields will be returned.',
enum: ['brief', 'full'],
type: 'string',
},
landing_company: {
description: "Deprecated - replaced by landing_company_short.",
description: 'Deprecated - replaced by landing_company_short.',
enum: [
"iom",
"malta",
"maltainvest",
"svg",
"virtual",
"vanuatu",
"champion",
"champion-virtual",
'iom',
'malta',
'maltainvest',
'svg',
'virtual',
'vanuatu',
'champion',
'champion-virtual',
],
type: "string",
type: 'string',
},
landing_company_short: {
description:
"[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.",
'[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.',
enum: [
"iom",
"malta",
"maltainvest",
"svg",
"virtual",
"vanuatu",
"champion",
"champion-virtual",
'iom',
'malta',
'maltainvest',
'svg',
'virtual',
'vanuatu',
'champion',
'champion-virtual',
],
type: "string",
type: 'string',
},
loginid: {
description:
"[Optional] The login id of the user. If left unspecified, it defaults to the initial authorized token's login id.",
pattern: '^[A-Za-z]+[0-9]+$',
type: 'string',
},
passthrough: {
description:
"[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.",
maxSize: 3500,
type: "object",
'[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.',
type: 'object',
},
product_type: {
description:
"[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.",
enum: ["basic"],
type: "string",
'[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.',
enum: ['basic'],
type: 'string',
},
req_id: {
description: "[Optional] Used to map request to response.",
type: "integer",
description: '[Optional] Used to map request to response.',
type: 'integer',
},
},
required: ["active_symbols"],
title: "Active Symbols (request)",
type: "object",
required: ['active_symbols'],
title: 'Active Symbols (request)',
type: 'object',
},
description:
"Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).",
'Retrieve a list of all currently active symbols (underlying markets upon which contracts are available for trading).',
properties: {
active_symbols: {
description:
"If you use `brief`, only a subset of fields will be returned.",
enum: ["brief", "full"],
type: "string",
description: 'If you use `brief`, only a subset of fields will be returned.',
enum: ['brief', 'full'],
type: 'string',
},
landing_company: {
description: "Deprecated - replaced by landing_company_short.",
description: 'Deprecated - replaced by landing_company_short.',
enum: [
"iom",
"malta",
"maltainvest",
"svg",
"virtual",
"vanuatu",
"champion",
"champion-virtual",
'iom',
'malta',
'maltainvest',
'svg',
'virtual',
'vanuatu',
'champion',
'champion-virtual',
],
type: "string",
type: 'string',
},
landing_company_short: {
description:
"[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.",
'[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.',
enum: [
"iom",
"malta",
"maltainvest",
"svg",
"virtual",
"vanuatu",
"champion",
"champion-virtual",
'iom',
'malta',
'maltainvest',
'svg',
'virtual',
'vanuatu',
'champion',
'champion-virtual',
],
type: "string",
type: 'string',
},
loginid: {
description:
"[Optional] The login id of the user. If left unspecified, it defaults to the initial authorized token's login id.",
pattern: '^[A-Za-z]+[0-9]+$',
type: 'string',
},
passthrough: {
description:
"[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.",
maxSize: 3500,
type: "object",
'[Optional] Used to pass data through the websocket, which may be retrieved via the `echo_req` output field. Maximum size is 3500 bytes.',
type: 'object',
},
product_type: {
description:
"[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.",
enum: ["basic"],
type: "string",
'[Optional] If you specify this field, only symbols that can be traded through that product type will be returned.',
enum: ['basic'],
type: 'string',
},
req_id: {
description: "[Optional] Used to map request to response.",
type: "integer",
description: '[Optional] Used to map request to response.',
type: 'integer',
},
},
required: ["active_symbols"],
title: "Active Symbols (request)",
type: "object",
required: ['active_symbols'],
title: 'Active Symbols (request)',
type: 'object',
});
});
});