File size: 1,008 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * @jest-environment jsdom
 */
import { renderHook, act } from '@testing-library/react';
import useCachedAnswers from '../use-cached-answers';

const localStorageMock = {
	getItem: jest.fn(),
	setItem: jest.fn(),
	removeItem: jest.fn(),
	clear: jest.fn(),
	length: 0,
	key: jest.fn(),
};

describe( 'useCachedAnswers', () => {
	beforeEach( () => {
		global.localStorage = localStorageMock;
	} );

	it( 'should update cached answers', () => {
		const surveyKey = 'test-key';
		const initialAnswers = { question1: [ 'option-1' ], question2: [ 'option-3' ] };
		const updatedAnswers = { question1: [ 'option-1', 'option-2' ], question2: [ 'option-3' ] };

		const { result } = renderHook( () => useCachedAnswers( surveyKey ) );

		act( () => {
			result.current.setAnswers( initialAnswers );
		} );

		expect( result.current.answers ).toEqual( initialAnswers );

		act( () => {
			result.current.setAnswers( updatedAnswers );
		} );

		expect( result.current.answers ).toEqual( updatedAnswers );
	} );
} );