UNPKG

@vergiss/chooks

Version:
59 lines (55 loc) 1.96 kB
"use strict"; import "core-js/modules/es.date.to-string"; import "core-js/modules/es.object.define-property"; Object.defineProperty(exports, "__esModule", { value: true }); var react_hooks_1 = require("@testing-library/react-hooks"); var index_1 = require("../index"); var mockTimestamp = 0; jest.spyOn(Date, 'now').mockImplementation(function () { return mockTimestamp; }); describe('useLocalStorage', function () { it('should be defined', function () { expect(index_1.useLocalStorage).toBeDefined(); }); it('should set a value to localStorage', function () { var key = 'test'; var hook = react_hooks_1.renderHook(function () { return index_1.useLocalStorage(key, 'hello'); }); var storage = hook.result.current[0]; expect(storage).toBe('hello'); expect(global.window.localStorage.getItem(key)).toBe('"hello"'); }); it('should update the storage', function () { var key = 'test'; var hook = react_hooks_1.renderHook(function () { return index_1.useLocalStorage(key, 'hello'); }); expect(hook.result.current[0]).toBe('hello'); expect(global.window.localStorage.getItem(key)).toBe('"hello"'); react_hooks_1.act(function () { hook.result.current[1]('world'); }); expect(hook.result.current[0]).toBe('world'); expect(global.window.localStorage.getItem(key)).toBe('"world"'); }); it('should get empty value after expiration age', function () { var key = 'text-expiration'; var hook = react_hooks_1.renderHook(function () { return index_1.useLocalStorage(key, 'value', { expireAge: 2000 }); }); expect(hook.result.current[0]).toBe('value'); expect(global.window.localStorage.getItem(key)).toBe('"value"'); react_hooks_1.act(function () { mockTimestamp += 2300; hook.rerender(); }); expect(hook.result.current[0]).toBe(null); expect(global.window.localStorage.getItem(key)).toBe(null); }); });