Skip to content

Commit

Permalink
fix: some failing tests in hooks now works
Browse files Browse the repository at this point in the history
  • Loading branch information
edenstrom committed Oct 6, 2021
1 parent f103564 commit c122f28
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
3 changes: 3 additions & 0 deletions libs/hooks/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ module.exports = {
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/hooks',
testEnvironment: 'jsdom',
clearMocks: true,
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
}
99 changes: 52 additions & 47 deletions libs/hooks/src/fake.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
import React from 'react'
import { act, renderHook } from '@testing-library/react-hooks'
import React from 'react'
import { ApiProvider } from '.'
import createStorage from './__mocks__/AsyncStorage'
import {
useCalendar,
useEtjanstChildren,
useClassmates,
useEtjanstChildren,
useMenu,
useNews,
useNotifications,
useSchedule,
useUser,
} from './hooks'
import store from './store'
import createStorage from './__mocks__/AsyncStorage'

const { default: init } = jest.requireActual('@skolplattformen/embedded-api')
const { default: init } = jest.requireActual(
'@skolplattformen/api-skolplattformen'
)

const wait = (ms) => new Promise((res) => setTimeout(res, ms))

describe('hooks with fake data', () => {
let api
let storage
const wrapper = ({ children }) => (
<ApiProvider api={api} storage={storage}>{children}</ApiProvider>
<ApiProvider api={api} storage={storage}>
{children}
</ApiProvider>
)
beforeEach(async () => {
api = init(() => { }, () => { })
api = init(
() => {},
() => {}
)
await api.login('121212121212')

storage = createStorage({})
})
it('does not use cache', async () => {
storage.cache.user = JSON.stringify({ user: 'cached' })
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useUser(), { wrapper })
const { result, waitForNextUpdate } = renderHook(() => useUser(), {
wrapper,
})

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -46,15 +52,15 @@ describe('hooks with fake data', () => {
firstName: 'Namn',
lastName: 'Namnsson',
isAuthenticated: true,
personalNumber: '195001182046',
})
})
})
it('returns user', async () => {
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useUser(), { wrapper })
const { result, waitForNextUpdate } = renderHook(() => useUser(), {
wrapper,
})

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -63,15 +69,16 @@ describe('hooks with fake data', () => {
firstName: 'Namn',
lastName: 'Namnsson',
isAuthenticated: true,
personalNumber: '195001182046',
})
})
})
it('returns child list', async () => {
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useEtjanstChildren(), { wrapper })
const { result, waitForNextUpdate } = renderHook(
() => useEtjanstChildren(),
{ wrapper }
)

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -83,14 +90,14 @@ describe('hooks with fake data', () => {
describe('data belonging to one child', () => {
let child
beforeEach(async () => {
[child] = await api.getChildren()
;[child] = await api.getChildren()
})
it('returns calendar', async () => {
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useCalendar(child), { wrapper })
const { result, waitForNextUpdate } = renderHook(
() => useCalendar(child),
{ wrapper }
)

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -100,10 +107,10 @@ describe('hooks with fake data', () => {
})
it('returns classmates', async () => {
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useClassmates(child), { wrapper })
const { result, waitForNextUpdate } = renderHook(
() => useClassmates(child),
{ wrapper }
)

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -113,10 +120,9 @@ describe('hooks with fake data', () => {
})
it('returns menu', async () => {
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useMenu(child), { wrapper })
const { result, waitForNextUpdate } = renderHook(() => useMenu(child), {
wrapper,
})

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -126,10 +132,9 @@ describe('hooks with fake data', () => {
})
it('returns news', async () => {
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useNews(child), { wrapper })
const { result, waitForNextUpdate } = renderHook(() => useNews(child), {
wrapper,
})

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -139,10 +144,10 @@ describe('hooks with fake data', () => {
})
it('returns notifications', async () => {
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useNotifications(child), { wrapper })
const { result, waitForNextUpdate } = renderHook(
() => useNotifications(child),
{ wrapper }
)

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -154,10 +159,10 @@ describe('hooks with fake data', () => {
const from = '2021-01-01'
const to = '2021-01-08'
await act(async () => {
const {
result,
waitForNextUpdate,
} = renderHook(() => useSchedule(child, from, to), { wrapper })
const { result, waitForNextUpdate } = renderHook(
() => useSchedule(child, from, to),
{ wrapper }
)

await waitForNextUpdate()
await waitForNextUpdate()
Expand All @@ -173,10 +178,10 @@ describe('hooks with fake data', () => {

const [child] = await api.getChildren()

const {
result,
waitForNextUpdate,
} = renderHook(() => useNotifications(child), { wrapper })
const { result, waitForNextUpdate } = renderHook(
() => useNotifications(child),
{ wrapper }
)

await waitForNextUpdate()
expect(result.current.status).toEqual('loaded')
Expand Down

0 comments on commit c122f28

Please sign in to comment.