-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for useInstanceVariable hook
- Loading branch information
1 parent
254d64c
commit 23bb596
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |