Skip to content

Commit

Permalink
test: remove async
Browse files Browse the repository at this point in the history
  • Loading branch information
Akurganow committed Jun 2, 2020
1 parent 38e05ef commit bedcd3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 16 additions & 12 deletions __tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -72,14 +72,14 @@ describe('Integration Tests', () => {

const testButton = render(<TestButton />)

await act(async () => { await waitFor(() => fireEvent.click(testButton.getByTestId(testButtonId))) })
act(() => { fireEvent.click(testButton.getByTestId(testButtonId)) })

const testComponent = render(<Component />)

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'
Expand Down Expand Up @@ -119,18 +119,24 @@ describe('Integration Tests', () => {

const testButton = render(<TestButton />)
const clearButton = render(<ClearButton />)
const testComponent = render(<Component />)

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(<Component />)
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'
Expand Down Expand Up @@ -182,8 +188,6 @@ describe('Integration Tests', () => {
fireEvent.click(testInitialButton.getByTestId(testInitialButtonId))
})

console.log(localStorage.__STORE__);

expect(testComponent.getByTestId(testComponentId).textContent).toBe(String(initialValue))
})
})
4 changes: 2 additions & 2 deletions __tests__/use-persisted-state.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit bedcd3c

Please sign in to comment.