Skip to content

Commit b6392bd

Browse files
committed
initial testing set up
1 parent 78a5407 commit b6392bd

15 files changed

+7525
-1643
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
import ContinuousSlider from '../src/components/ContinuousSlider'; // Adjust the import path as necessary
5+
import '@testing-library/jest-dom';
6+
7+
describe('ContinuousSlider', () => {
8+
it('renders correctly with given props', () => {
9+
const handleChange = jest.fn();
10+
const { asFragment } = render(<ContinuousSlider value={30} maxValue={100} onChange={handleChange} />);
11+
12+
const slider = screen.getByRole('slider');
13+
expect(slider).toBeInTheDocument();
14+
expect(slider).toHaveAttribute('aria-valuemax', '100');
15+
expect(slider).toHaveAttribute('aria-valuenow', '30');
16+
expect(asFragment()).toMatchSnapshot();
17+
});
18+
19+
it('calls onChange with the new value when changed', async () => {
20+
const handleChange = jest.fn();
21+
render(<ContinuousSlider value={30} maxValue={100} onChange={handleChange} />);
22+
23+
// Assuming the slider allows keyboard input, you can simulate changes like this
24+
const slider = screen.getByRole('slider');
25+
await userEvent.click(slider);
26+
await userEvent.keyboard('{arrowright}'); // Simulates pressing the right arrow key to increase the value
27+
28+
expect(handleChange).toHaveBeenCalledWith(expect.any(Number));
29+
});
30+
31+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import DiffTab from '../src/containers/DiffTab';
4+
import '@testing-library/jest-dom';
5+
import { QueryDisplay } from '../src/types';
6+
import exp from 'constants';
7+
8+
describe('DiffTab Component', () => {
9+
const queryDisplayMock: QueryDisplay[][] = [
10+
[
11+
{
12+
queryKey: 'Query 1',
13+
queryData: { some: 'data' }
14+
},
15+
{
16+
queryKey: 'Query 2',
17+
queryData: { more: 'data' }
18+
}
19+
],
20+
[
21+
{
22+
queryKey: 'Query 1',
23+
queryData: { some: 'different data' }
24+
}
25+
]
26+
];
27+
28+
it('renders correctly with initial state', () => {
29+
const { asFragment } = render(<DiffTab queryDisplay={queryDisplayMock} currentIndex={0} />);
30+
expect(screen.getByLabelText(/Hide Unchanged Properties/i)).toBeInTheDocument();
31+
expect(screen.getByText(/Query 1/i)).toBeInTheDocument();
32+
expect(asFragment()).toMatchSnapshot();
33+
});
34+
35+
it('toggles isHidden state on switch toggle', () => {
36+
const { asFragment } = render(<DiffTab queryDisplay={queryDisplayMock} currentIndex={0} />);
37+
fireEvent.click(screen.getByRole('checkbox')); // Clicks the switch
38+
expect(screen.getByLabelText(/Show Unchanged Properties/i)).toBeInTheDocument();
39+
expect(asFragment()).toMatchSnapshot();
40+
});
41+
42+
it('renders JsonDiff component with correct props based on isHidden state', () => {
43+
const { rerender, asFragment } = render(<DiffTab queryDisplay={queryDisplayMock} currentIndex={1} />);
44+
let switchControl = screen.getByRole('checkbox');
45+
46+
// Initially, isHidden should be false, so "Hide" should be in the label
47+
expect(switchControl).not.toBeChecked();
48+
expect(screen.getByText(/Hide Unchanged Properties/i)).toBeInTheDocument();
49+
expect(asFragment()).toMatchSnapshot();
50+
51+
// Toggle the switch to change isHidden state
52+
fireEvent.click(switchControl);
53+
// Rerendering is necessary only if the component's response to state changes cannot be detected without it
54+
rerender(<DiffTab queryDisplay={queryDisplayMock} currentIndex={1} />);
55+
56+
// Now, isHidden should be true, so "Show" should be in the label
57+
expect(screen.getByLabelText(/Show Unchanged Properties/i)).toBeInTheDocument();
58+
expect(asFragment()).toMatchSnapshot();
59+
});
60+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import JsonDiff from '../src/components/JsonDiff';
5+
6+
describe('JsonDiff Component', () => {
7+
it('displays initial state message for empty currentJson and snapshot', () => {
8+
const { getByText, asFragment } = render(
9+
<JsonDiff
10+
oldJson={undefined}
11+
currentJson=""
12+
queryKey="testQueryKey"
13+
isHidden={false}
14+
/>
15+
);
16+
expect(getByText(/Initial state - no comparison available/i)).toBeInTheDocument();
17+
expect(asFragment()).toMatchSnapshot();
18+
});
19+
20+
it('displays a diff when there are differences between oldJson and currentJson and snapshot', () => {
21+
const oldJson = { key: "oldValue" };
22+
const currentJson = { key: "newValue" };
23+
const { container, asFragment } = render(
24+
<JsonDiff
25+
oldJson={oldJson}
26+
currentJson={currentJson}
27+
queryKey="testQueryKey"
28+
isHidden={false}
29+
/>
30+
);
31+
// Assert that the diff is displayed
32+
expect(container.querySelector('.jsondiffpatch-delta')).not.toBeNull();
33+
expect(asFragment()).toMatchSnapshot();
34+
});
35+
36+
37+
it('displays no changes message when oldJson and currentJson are the same', () => {
38+
const json = { key: "value" };
39+
const { getByText, asFragment } = render(
40+
<JsonDiff
41+
oldJson={json}
42+
currentJson={json}
43+
queryKey="testQueryKey"
44+
isHidden={false}
45+
/>
46+
);
47+
expect(getByText(/QueryKey data not modified on this state change/i)).toBeInTheDocument();
48+
expect(asFragment()).toMatchSnapshot();
49+
});
50+
51+
it('toggles visibility based on isHidden prop', () => {
52+
const json = { key: "value" };
53+
const newJson = { key: "newValue" };
54+
const { container, rerender, asFragment } = render(
55+
<JsonDiff
56+
oldJson={json}
57+
currentJson={newJson}
58+
queryKey="testQueryKey"
59+
isHidden={true}
60+
/>
61+
);
62+
// Initially, the class indicating hidden changes should be present
63+
expect(container.querySelector('.jsondiffpatch-unchanged-hidden')).not.toBeNull();
64+
expect(asFragment()).toMatchSnapshot();
65+
66+
// Rerender with isHidden = false and check if the class is removed
67+
rerender(
68+
<JsonDiff
69+
oldJson={json}
70+
currentJson={newJson}
71+
queryKey="testQueryKey"
72+
isHidden={false}
73+
/>
74+
);
75+
expect(container.querySelector('.jsondiffpatch-unchanged-hidden')).toBeNull();
76+
expect(asFragment()).toMatchSnapshot();
77+
});
78+
79+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import JsonFormatter from '../src/components/JsonFormatter';
4+
5+
describe('JsonFormatter component', () => {
6+
it('renders without crashing and snapshot', () => {
7+
const { asFragment } = render(<JsonFormatter
8+
key={1}
9+
queryKey={'[posts-one]'}
10+
jsonData={{val: 'test', arr: [1,2,3]}}
11+
/>);
12+
13+
expect(asFragment()).toMatchSnapshot();
14+
});
15+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ContinuousSlider renders correctly with given props 1`] = `
4+
<DocumentFragment>
5+
<span
6+
class="MuiSlider-root MuiSlider-colorPrimary MuiSlider-sizeMedium css-187mznn-MuiSlider-root"
7+
>
8+
<span
9+
class="MuiSlider-rail css-14pt78w-MuiSlider-rail"
10+
/>
11+
<span
12+
class="MuiSlider-track css-1gv0vcd-MuiSlider-track"
13+
style="left: 0%; width: 30%;"
14+
/>
15+
<span
16+
class="MuiSlider-thumb MuiSlider-thumbSizeMedium MuiSlider-thumbColorPrimary MuiSlider-thumb MuiSlider-thumbSizeMedium MuiSlider-thumbColorPrimary css-eg0mwd-MuiSlider-thumb"
17+
data-index="0"
18+
style="left: 30%;"
19+
>
20+
<input
21+
aria-labelledby="continuous-slider"
22+
aria-orientation="horizontal"
23+
aria-valuemax="100"
24+
aria-valuemin="0"
25+
aria-valuenow="30"
26+
data-index="0"
27+
max="100"
28+
min="0"
29+
step="1"
30+
style="border: 0px; height: 100%; margin: -1px; overflow: hidden; padding: 0px; position: absolute; white-space: nowrap; width: 100%; direction: ltr;"
31+
type="range"
32+
value="30"
33+
/>
34+
</span>
35+
</span>
36+
</DocumentFragment>
37+
`;

0 commit comments

Comments
 (0)