diff --git a/__tests__/index.test.tsx b/__tests__/index.test.tsx index 9f2cc660..2f09c779 100644 --- a/__tests__/index.test.tsx +++ b/__tests__/index.test.tsx @@ -1,6 +1,6 @@ import React from 'react' import createPersistedState from '../src' -import { render, fireEvent, cleanup, act, waitFor } from '@testing-library/react' +import { render, fireEvent, cleanup, act } from '@testing-library/react' const [usePersistedState, clear] = createPersistedState('test') @@ -10,7 +10,7 @@ describe('Integration Tests', () => { localStorage.clear() }) - it('Component should rerender from change to local storage', async () => { + it('Component should rerender from change to local storage', () => { const initialValue = 0 const testComponentId = 'test_count_component' const testButtonId = 'test_count_button' @@ -45,7 +45,7 @@ describe('Integration Tests', () => { expect(testComponent.getByTestId(testComponentId).textContent).toBe(String(initialValue + 1)) }) - it('Component should render with persisted state', async () => { + it('Component should render with persisted state', () => { const initialValue = 0 const testComponentId = 'test_count_component' const testButtonId = 'test_count_button' @@ -72,14 +72,14 @@ describe('Integration Tests', () => { const testButton = render() - await act(async () => { await waitFor(() => fireEvent.click(testButton.getByTestId(testButtonId))) }) + act(() => { fireEvent.click(testButton.getByTestId(testButtonId)) }) const testComponent = render() expect(testComponent.getByTestId(testComponentId).textContent).toBe(String(initialValue + 1)) }) - it('Should clear persisted state', async () => { + it('Should clear persisted state', () => { const initialValue = 0 const testComponentId = 'test_count_component' const testButtonId = 'test_count_button' @@ -119,18 +119,24 @@ describe('Integration Tests', () => { const testButton = render() const clearButton = render() + const testComponent = render() + + expect(testComponent.getByTestId(testComponentId).textContent).toBe(String(initialValue)) - await act(async () => { - await waitFor(() => fireEvent.click(testButton.getByTestId(testButtonId))) - await waitFor(() => fireEvent.click(clearButton.getByTestId(clearButtonId))) + act(() => { + fireEvent.click(testButton.getByTestId(testButtonId)) }) - const testComponent = render() + expect(testComponent.getByTestId(testComponentId).textContent).toBe(String(initialValue + 1)) + + act(() => { + fireEvent.click(clearButton.getByTestId(clearButtonId)) + }) expect(testComponent.getByTestId(testComponentId).textContent).toBe(String(initialValue)) }) - it('Should change state when set initial value', async () => { + it('Should change state when set initial value', () => { const initialValue = 0 const testComponentId = 'test_count_component' const testButtonId = 'test_count_button' @@ -182,8 +188,6 @@ describe('Integration Tests', () => { fireEvent.click(testInitialButton.getByTestId(testInitialButtonId)) }) - console.log(localStorage.__STORE__); - expect(testComponent.getByTestId(testComponentId).textContent).toBe(String(initialValue)) }) }) diff --git a/__tests__/use-persisted-state.test.tsx b/__tests__/use-persisted-state.test.tsx index 4bdfc7f3..4ec61b5d 100644 --- a/__tests__/use-persisted-state.test.tsx +++ b/__tests__/use-persisted-state.test.tsx @@ -4,11 +4,11 @@ import { renderHook, cleanup, act } from '@testing-library/react-hooks' const [usePersistedState, clear] = createPersistedState('test') describe('hook defined correctly', () => { - afterEach(() => { + beforeEach(() => { cleanup() clear() localStorage.clear() - }); + }) it('is callable', () => { const { result } = renderHook(() => usePersistedState('foo', 'bar'))