-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
legger til tester for timer og minutter/desimaler conversion
- Loading branch information
Showing
5 changed files
with
227 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { renderWithIntl } from '../../testUtils'; | ||
import FaktiskOgNormalTid from '../../../app/components/timefoering/FaktiskOgNormalTid'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
describe('FaktiskOgNormalTid', () => { | ||
const mockLagre = jest.fn(); | ||
const mockToggleModal = jest.fn(); | ||
const mockClearSelectedDates = jest.fn(); | ||
|
||
it('renderWithIntls without crashing', () => { | ||
renderWithIntl(<FaktiskOgNormalTid lagre={mockLagre} toggleModal={mockToggleModal} selectedDates={[]} />); | ||
}); | ||
|
||
it('updates value when input is changed', async () => { | ||
const { getAllByLabelText } = renderWithIntl( | ||
<FaktiskOgNormalTid lagre={mockLagre} toggleModal={mockToggleModal} selectedDates={[]} />, | ||
); | ||
const input = getAllByLabelText('Timer')[0]; | ||
await userEvent.type(input, '7'); | ||
|
||
expect(input.value).toBe('7'); | ||
}); | ||
|
||
it('toggles time format and converts time correctly', async () => { | ||
const { getByLabelText, getByText, getAllByLabelText } = renderWithIntl( | ||
<FaktiskOgNormalTid | ||
lagre={mockLagre} | ||
toggleModal={mockToggleModal} | ||
selectedDates={[]} | ||
clearSelectedDates={mockClearSelectedDates} | ||
/>, | ||
); | ||
// Initialize the time | ||
const normalArbeidstidTimer = getAllByLabelText('Timer')[0]; | ||
const normalArbeidstidMinutter = getAllByLabelText('Minutter')[0]; | ||
await userEvent.type(normalArbeidstidTimer, '2'); | ||
await userEvent.type(normalArbeidstidMinutter, '30'); | ||
|
||
// Toggle to decimal format | ||
const desimaltallToggle = getByText('Desimaltall'); | ||
await userEvent.click(desimaltallToggle); | ||
|
||
// Verify time has been converted to decimal | ||
const normalArbeidstidDesimaler = getByLabelText('Normal arbeidstid'); | ||
expect(normalArbeidstidDesimaler.value).toBe('2.5'); | ||
|
||
// Toggle back to hours and minutes format | ||
const timerOgMinutterToggle = getByText('Timer og minutter'); | ||
await userEvent.click(timerOgMinutterToggle); | ||
|
||
// Verify time has been converted back to hours and minutes | ||
expect(normalArbeidstidTimer.value).toBe('2'); | ||
expect(normalArbeidstidMinutter.value).toBe('30'); | ||
}); | ||
|
||
it('converts correctly after changing numbers', async () => { | ||
const { getByLabelText, getByRole, getAllByLabelText } = renderWithIntl( | ||
<FaktiskOgNormalTid | ||
lagre={mockLagre} | ||
toggleModal={mockToggleModal} | ||
selectedDates={[]} | ||
clearSelectedDates={mockClearSelectedDates} | ||
/>, | ||
); | ||
|
||
// Initialize the time | ||
const normalArbeidstidTimer = getAllByLabelText('Timer')[0]; | ||
const normalArbeidstidMinutter = getAllByLabelText('Minutter')[0]; | ||
await userEvent.type(normalArbeidstidTimer, '3'); | ||
await userEvent.type(normalArbeidstidMinutter, '45'); | ||
|
||
// Toggle to decimal format | ||
const desimaltallToggle = getByRole('radio', { name: /Desimaltall/i }); | ||
await userEvent.click(desimaltallToggle); | ||
|
||
// Check that time has been converted to decimal format | ||
const normalArbeidstidDesimaler = getByLabelText('Normal arbeidstid'); | ||
expect(normalArbeidstidDesimaler.value).toBe('3.75'); | ||
|
||
await userEvent.clear(normalArbeidstidDesimaler); | ||
await userEvent.type(normalArbeidstidDesimaler, '4'); | ||
|
||
// Verify time has been converted to decimal format | ||
const normalArbeidstidDesimalerAfter = getByLabelText('Normal arbeidstid'); | ||
|
||
await waitFor(() => { | ||
expect(normalArbeidstidDesimalerAfter.value).toBe('4'); | ||
}); | ||
// Toggle back to hours and minutes format | ||
const timerOgMinutterToggle = getByRole('radio', { name: /Timer og minutter/i }); | ||
await userEvent.click(timerOgMinutterToggle); | ||
|
||
// Verify time has been converted back to hours and minutes | ||
|
||
const normalArbeidstidTimer2 = getAllByLabelText('Timer')[0]; | ||
const normalArbeidstidMinutter2 = getAllByLabelText('Minutter')[0]; | ||
|
||
await waitFor(() => { | ||
expect(normalArbeidstidTimer2.value).toBe('4'); | ||
expect(normalArbeidstidMinutter2.value).toBe('0'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import TimerOgMinutter from '../../../app/components/timefoering/TimerOgMinutter'; | ||
|
||
describe('TimerOgMinutter', () => { | ||
const mockOnChangeTimer = jest.fn(); | ||
const mockOnChangeMinutter = jest.fn(); | ||
const mockOnBlur = jest.fn(); | ||
|
||
it('renders without crashing', () => { | ||
render( | ||
<TimerOgMinutter | ||
label="Test Label" | ||
onChangeTimer={mockOnChangeTimer} | ||
onChangeMinutter={mockOnChangeMinutter} | ||
onBlur={mockOnBlur} | ||
timer="" | ||
minutter="" | ||
/>, | ||
); | ||
}); | ||
|
||
it('calls onChangeTimer when timer input is changed', () => { | ||
const { getByLabelText } = render( | ||
<TimerOgMinutter | ||
label="Test Label" | ||
onChangeTimer={mockOnChangeTimer} | ||
onChangeMinutter={mockOnChangeMinutter} | ||
onBlur={mockOnBlur} | ||
timer="" | ||
minutter="" | ||
/>, | ||
); | ||
const input = getByLabelText('Timer'); | ||
fireEvent.change(input, { target: { value: '7' } }); | ||
expect(mockOnChangeTimer).toHaveBeenCalledWith('7'); | ||
}); | ||
|
||
it('calls onChangeMinutter when minutter input is changed', () => { | ||
const { getByLabelText } = render( | ||
<TimerOgMinutter | ||
label="Test Label" | ||
onChangeTimer={mockOnChangeTimer} | ||
onChangeMinutter={mockOnChangeMinutter} | ||
onBlur={mockOnBlur} | ||
timer="" | ||
minutter="" | ||
/>, | ||
); | ||
const input = getByLabelText('Minutter'); | ||
fireEvent.change(input, { target: { value: '30' } }); | ||
expect(mockOnChangeMinutter).toHaveBeenCalledWith('30'); | ||
}); | ||
|
||
it('calls onBlur when input loses focus', () => { | ||
const { getByLabelText } = render( | ||
<TimerOgMinutter | ||
label="Test Label" | ||
onChangeTimer={mockOnChangeTimer} | ||
onChangeMinutter={mockOnChangeMinutter} | ||
onBlur={mockOnBlur} | ||
timer="" | ||
minutter="" | ||
/>, | ||
); | ||
const input = getByLabelText('Timer'); | ||
fireEvent.blur(input); | ||
expect(mockOnBlur).toHaveBeenCalled(); | ||
}); | ||
|
||
it('displays error message when error prop is provided', () => { | ||
const { getByText } = render( | ||
<TimerOgMinutter | ||
label="Test Label" | ||
onChangeTimer={mockOnChangeTimer} | ||
onChangeMinutter={mockOnChangeMinutter} | ||
onBlur={mockOnBlur} | ||
timer="" | ||
minutter="" | ||
error="Test error message" | ||
/>, | ||
); | ||
expect(getByText('Test error message')).toBeInTheDocument(); | ||
}); | ||
}); |