Skip to content

Commit

Permalink
Add a test for useInstanceVariable hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Jun 6, 2024
1 parent 254d64c commit 23bb596
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/web/hooks/__tests__/useInstanceVariable.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/* eslint-disable react/prop-types */

import {describe, test, expect, testing} from '@gsa/testing';

import {useCallback} from 'react';

import {fireEvent, rendererWith, screen} from 'web/utils/testing';

import useInstanceVariable from '../useInstanceVariable';

const TestComponent = ({callback}) => {
const someVariable = useInstanceVariable({value: 1});
const changeValue = useCallback(() => {
someVariable.value = 2;
callback(someVariable.value);
}, [someVariable, callback]);
return (
<div>
<div data-testid="t1">{someVariable.value}</div>
<button data-testid="changeValue" onClick={changeValue} />
</div>
);
};

describe('useInstanceVariable tests', () => {
test('should render the value', () => {
const callback = testing.fn();
const {render} = rendererWith();

render(<TestComponent callback={callback} />);

const t1 = screen.getByTestId('t1');
expect(t1).toHaveTextContent('1');
const b1 = screen.getByTestId('changeValue');
fireEvent.click(b1);
expect(t1).toHaveTextContent('1');

expect(callback).toHaveBeenCalledWith(2);
});
});

0 comments on commit 23bb596

Please sign in to comment.