-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests/Add create survey unit tests (#205)
- Loading branch information
Showing
4 changed files
with
141 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
115 changes: 115 additions & 0 deletions
115
src/features/surveys/managers/createSurveyManager.test.ts
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,115 @@ | ||
import { useCreateSurveyManager } from 'features/surveys/managers/createSurveyManager'; | ||
import { v4 } from 'uuid'; | ||
import { act, renderHook, waitFor } from '@testing-library/react'; | ||
import { defaultQuestions } from 'shared/constants/surveysConfig'; | ||
|
||
jest.mock('next/router', () => require('next-router-mock')); | ||
|
||
const NEW_QUESTION_TITLE = 'new question'; | ||
|
||
const NEW_SURVEY_TITLE = 'new survey title'; | ||
|
||
const setUp = () => { | ||
const { result } = renderHook(() => useCreateSurveyManager()); | ||
|
||
act(() => { | ||
result.current.addQuestion({ | ||
id: v4(), | ||
type: 'INPUT', | ||
title: NEW_QUESTION_TITLE, | ||
options: [], | ||
isRequired: false, | ||
}); | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
describe('useCreateSurveyManager tests', () => { | ||
it('should add questions correctly', async () => { | ||
const result = setUp(); | ||
act(() => { | ||
result.current.addQuestion({ | ||
id: v4(), | ||
type: 'INPUT', | ||
title: 'test', | ||
options: [], | ||
isRequired: false, | ||
}); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.questions.length).toBe(defaultQuestions.length + 2); | ||
}); | ||
}); | ||
|
||
it('should remove questions correctly', async () => { | ||
const result = setUp(); | ||
|
||
act(() => { | ||
result.current.removeQuestion(0); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.questions.length).toBe(defaultQuestions.length); | ||
}); | ||
await waitFor(() => { | ||
expect(result.current.questions[1].title).toBe(NEW_QUESTION_TITLE); | ||
}); | ||
}); | ||
|
||
it('should update survey title correctly', async () => { | ||
const result = setUp(); | ||
|
||
const mockEvent = { | ||
target: { | ||
value: NEW_SURVEY_TITLE, | ||
}, | ||
} as React.ChangeEvent<HTMLInputElement>; | ||
|
||
act(() => { | ||
result.current.handleChangeTitle(mockEvent); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.title).toBe(NEW_SURVEY_TITLE); | ||
}); | ||
}); | ||
|
||
it('should update question by index correctly', async () => { | ||
const result = setUp(); | ||
|
||
const updatedTitle = 'updated title'; | ||
|
||
act(() => { | ||
result.current.updateQuestion(updatedTitle, 0); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.questions[0].title).toBe(updatedTitle); | ||
}); | ||
}); | ||
|
||
it('should toggle question required', async () => { | ||
const result = setUp(); | ||
const lastQuestionIndex = result.current.questions.length - 1; | ||
|
||
act(() => { | ||
result.current.updateQuestionRequired(lastQuestionIndex); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.questions[lastQuestionIndex].isRequired).toBe(true); | ||
}); | ||
|
||
act(() => { | ||
result.current.updateQuestionRequired(lastQuestionIndex); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.questions[lastQuestionIndex].isRequired).toBe( | ||
false | ||
); | ||
}); | ||
}); | ||
}); |
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
cfe5338
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.
Successfully deployed to the following URLs:
formslab – ./
formslab-ryczko.vercel.app
formslab-git-main-ryczko.vercel.app
formslab.vercel.app