-
Notifications
You must be signed in to change notification settings - Fork 0
Story/am 437 laadscherm bij lang wachten uitbreiden #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0d5250d
63d6e76
32266af
81ada35
4f13bad
3a1028e
18239c8
9f0ea62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import {act, render} from '@testing-library/react-native' | ||
| import {type ComponentProps} from 'react' | ||
| import {PleaseWait} from '@/components/ui/feedback/PleaseWait' | ||
| import {StoreProvider} from '@/providers/store.provider' | ||
|
|
||
| describe('PleaseWait', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers() | ||
| jest.setSystemTime(new Date('2026-01-01T12:00:00.000Z')) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers() | ||
| }) | ||
|
|
||
| const renderPleaseWait = ( | ||
| props: ComponentProps<typeof PleaseWait> | Record<string, unknown>, | ||
| ) => | ||
| render( | ||
| <StoreProvider> | ||
| <PleaseWait {...(props as ComponentProps<typeof PleaseWait>)} /> | ||
| </StoreProvider>, | ||
| ) | ||
|
|
||
| it('renders the spinner with a valid testID', () => { | ||
| const {getByTestId, queryByTestId} = renderPleaseWait({ | ||
| testID: 'PleaseWait', | ||
| }) | ||
|
|
||
| expect(getByTestId('PleaseWait')).toBeTruthy() | ||
| expect(queryByTestId('PleaseWaitFeedbackPhrase')).toBeNull() | ||
| }) | ||
|
|
||
| it('does not render feedback when showFeedback is undefined or null', () => { | ||
| const undefinedPropsRender = renderPleaseWait({showFeedback: undefined}) | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(16000) | ||
| }) | ||
|
|
||
| expect( | ||
| undefinedPropsRender.queryByTestId('PleaseWaitFeedbackPhrase'), | ||
| ).toBeNull() | ||
|
|
||
| undefinedPropsRender.unmount() | ||
|
|
||
| const nullPropsRender = renderPleaseWait({showFeedback: null}) | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(16000) | ||
| }) | ||
|
|
||
| expect(nullPropsRender.queryByTestId('PleaseWaitFeedbackPhrase')).toBeNull() | ||
| }) | ||
|
|
||
| it('shows the first feedback message after five seconds when showFeedback is true, and still at 14.9 seconds', () => { | ||
| const {queryByText} = renderPleaseWait({showFeedback: true}) | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(5000) | ||
| }) | ||
|
|
||
| expect(queryByText('Gegevens worden geladen')).toBeTruthy() | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(9999) | ||
| }) | ||
|
|
||
| expect(queryByText('Gegevens worden geladen')).toBeTruthy() | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(1) | ||
| }) | ||
|
|
||
| expect(queryByText('Gegevens worden geladen')).not.toBeTruthy() | ||
| expect( | ||
| queryByText('Dit duurt langer dan normaal. \n We zijn nog bezig.'), | ||
| ).toBeTruthy() | ||
| }) | ||
|
|
||
| it('shows the second feedback message after fifteen seconds when startedTimeStamp is valid, and infinitely beyond that', () => { | ||
| const {queryByText} = renderPleaseWait({startedTimeStamp: Date.now()}) | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(5000) | ||
| }) | ||
|
|
||
| expect( | ||
| queryByText('Dit duurt langer dan normaal. \n We zijn nog bezig.'), | ||
| ).not.toBeTruthy() | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(10000) | ||
| }) | ||
|
|
||
| expect( | ||
| queryByText('Dit duurt langer dan normaal. \n We zijn nog bezig.'), | ||
| ).toBeTruthy() | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(60000) | ||
| }) | ||
|
|
||
| expect( | ||
| queryByText('Dit duurt langer dan normaal. \n We zijn nog bezig.'), | ||
| ).toBeTruthy() | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,21 +1,94 @@ | ||||||
| import {useEffect, useMemo, useRef, useState} from 'react' | ||||||
| import {Box} from '@/components/ui/containers/Box' | ||||||
| import {Center} from '@/components/ui/layout/Center' | ||||||
| import {Column} from '@/components/ui/layout/Column' | ||||||
| import {Icon} from '@/components/ui/media/Icon' | ||||||
| import {Phrase} from '@/components/ui/text/Phrase' | ||||||
| import {type TestProps} from '@/components/ui/types' | ||||||
| import {dayjs} from '@/utils/datetime/dayjs' | ||||||
|
|
||||||
| type Props = { | ||||||
| grow?: boolean | ||||||
| } & TestProps | ||||||
|
|
||||||
| export const PleaseWait = ({grow, testID}: Props) => ( | ||||||
| <Center grow={grow}> | ||||||
| <Box> | ||||||
| <Icon | ||||||
| color="link" | ||||||
| name="spinner" | ||||||
| size="lg" | ||||||
| testID={testID} | ||||||
| /> | ||||||
| </Box> | ||||||
| </Center> | ||||||
| ) | ||||||
| } & TestProps & | ||||||
| Or< | ||||||
| { | ||||||
| /** | ||||||
| * Add a timestamp to start a timer which shows textual loading timeout feedback. | ||||||
| */ | ||||||
| startedTimeStamp?: number | ||||||
| }, | ||||||
| { | ||||||
| /** | ||||||
| * Starts a timer from time of mount (as ref) and show textual loading timeout feedback. | ||||||
| */ | ||||||
| showFeedback?: true | ||||||
| } | ||||||
| > | ||||||
|
|
||||||
| const FIRST_TIMEOUT_VALUE = 5 | ||||||
| const SECOND_TIMEOUT_VALUE = 15 | ||||||
|
|
||||||
| const getElapsedTimeFeedback = (elapsedTime: number) => { | ||||||
| if ( | ||||||
| elapsedTime >= FIRST_TIMEOUT_VALUE && | ||||||
| elapsedTime < SECOND_TIMEOUT_VALUE | ||||||
| ) { | ||||||
| return 'Gegevens worden geladen' | ||||||
| } else if (elapsedTime >= SECOND_TIMEOUT_VALUE) { | ||||||
| return 'Dit duurt langer dan normaal. \n We zijn nog bezig.' | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export const PleaseWait = ({ | ||||||
| grow, | ||||||
| startedTimeStamp, | ||||||
| showFeedback, | ||||||
| testID, | ||||||
| }: Props) => { | ||||||
| const [elapsedTime, setElapsedTime] = useState(0) | ||||||
| const startTimeRef = useRef<number | null>(showFeedback ? Date.now() : null) | ||||||
| useEffect(() => { | ||||||
| const countFrom = startedTimeStamp || startTimeRef.current | ||||||
|
|
||||||
| if (!countFrom) { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| const interval = setInterval(() => { | ||||||
| setElapsedTime(Math.abs(dayjs(countFrom).diff())) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Als je als unit second meegeeft, dan hoef je niet door 1000 te delen later:
Suggested change
|
||||||
| }, 1000) | ||||||
|
|
||||||
| return () => { | ||||||
| clearInterval(interval) | ||||||
| } | ||||||
|
Copilot marked this conversation as resolved.
|
||||||
| }, [startedTimeStamp, startTimeRef]) | ||||||
|
|
||||||
| const elapsedSeconds = Math.floor(elapsedTime / 1000) | ||||||
|
|
||||||
| const feedback = useMemo( | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ik denk dat het berekenen van de cache check (van useMemo om te kijken of de oude waarde gebruikt moet worden) meer rekenkracht kost hier dan getElapsedTimeFeedback zelf uitvoeren |
||||||
| () => getElapsedTimeFeedback(elapsedSeconds), | ||||||
| [elapsedSeconds], | ||||||
| ) | ||||||
|
|
||||||
| return ( | ||||||
| <Center grow={grow}> | ||||||
| <Box> | ||||||
| <Column gutter="md"> | ||||||
| <Icon | ||||||
| color="link" | ||||||
| name="spinner" | ||||||
| size="lg" | ||||||
| testID={testID} | ||||||
| /> | ||||||
| {!!feedback && ( | ||||||
| <Phrase | ||||||
| testID="PleaseWaitFeedbackPhrase" | ||||||
| textAlign="center"> | ||||||
| {feedback} | ||||||
| </Phrase> | ||||||
| )} | ||||||
| </Column> | ||||||
| </Box> | ||||||
| </Center> | ||||||
| ) | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
de tweede regel begint nu met een spatie