From e9dd33ea67cfe4cdb3e6fa0a4e92b27dc0c5f7b8 Mon Sep 17 00:00:00 2001 From: Ryczko Date: Wed, 9 Aug 2023 20:07:57 +0200 Subject: [PATCH] Tests/Add create survey unit tests --- package-lock.json | 18 +++ package.json | 7 +- .../managers/createSurveyManager.test.ts | 115 ++++++++++++++++++ .../surveys/managers/createSurveyManager.ts | 14 +-- 4 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 src/features/surveys/managers/createSurveyManager.test.ts diff --git a/package-lock.json b/package-lock.json index 7218bb2f..83084aa8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -75,6 +75,7 @@ "husky": "^8.0.0", "jest": "^29.5.0", "jest-environment-jsdom": "^29.5.0", + "next-router-mock": "^0.9.7", "postcss": "^8.4.21", "prisma": "^4.14.0", "start-server-and-test": "^2.0.0", @@ -10119,6 +10120,16 @@ "uuid": "dist/bin/uuid" } }, + "node_modules/next-router-mock": { + "version": "0.9.7", + "resolved": "https://registry.npmjs.org/next-router-mock/-/next-router-mock-0.9.7.tgz", + "integrity": "sha512-y5ioLCIsdkJKwcoPnrUyocNEJT22RK4wKSg6LO0Q2XkBBvkYprEWy5FiCZt+CA8+qpfxpBlNca76F+glEohbRA==", + "dev": true, + "peerDependencies": { + "next": ">=10.0.0", + "react": ">=17.0.0" + } + }, "node_modules/next-translate": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/next-translate/-/next-translate-2.0.5.tgz", @@ -20635,6 +20646,13 @@ } } }, + "next-router-mock": { + "version": "0.9.7", + "resolved": "https://registry.npmjs.org/next-router-mock/-/next-router-mock-0.9.7.tgz", + "integrity": "sha512-y5ioLCIsdkJKwcoPnrUyocNEJT22RK4wKSg6LO0Q2XkBBvkYprEWy5FiCZt+CA8+qpfxpBlNca76F+glEohbRA==", + "dev": true, + "requires": {} + }, "next-translate": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/next-translate/-/next-translate-2.0.5.tgz", diff --git a/package.json b/package.json index 1ff86a96..6440a156 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "build": "next build", "start": "next start", "test": "jest", + "test:cov": "jest --coverage", "test-ci": "cross-env CI=1 jest --ci", "test-watch": "jest test --watch", "lint": "tsc && eslint --max-warnings=0 --ext .js,.ts,.tsx src/", @@ -14,10 +15,7 @@ "prepare": "husky install", "cypress": "cypress open", "postinstall": "prisma generate", - "cypress:headless": "cypress run", - "e2e": "start-server-and-test dev http://localhost:3000 cypress", - "e2e:headless": "start-server-and-test dev http://localhost:3000 cypress:headless", - "e2e:headless:ci": "start-server-and-test http://localhost:3000 cypress:headless" + "cypress:headless": "cypress run" }, "dependencies": { "@emoji-mart/data": "^1.1.2", @@ -86,6 +84,7 @@ "husky": "^8.0.0", "jest": "^29.5.0", "jest-environment-jsdom": "^29.5.0", + "next-router-mock": "^0.9.7", "postcss": "^8.4.21", "prisma": "^4.14.0", "start-server-and-test": "^2.0.0", diff --git a/src/features/surveys/managers/createSurveyManager.test.ts b/src/features/surveys/managers/createSurveyManager.test.ts new file mode 100644 index 00000000..cb8a54a0 --- /dev/null +++ b/src/features/surveys/managers/createSurveyManager.test.ts @@ -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; + + 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 + ); + }); + }); +}); diff --git a/src/features/surveys/managers/createSurveyManager.ts b/src/features/surveys/managers/createSurveyManager.ts index de1d8915..7976eede 100644 --- a/src/features/surveys/managers/createSurveyManager.ts +++ b/src/features/surveys/managers/createSurveyManager.ts @@ -17,10 +17,13 @@ export interface Question { export const useCreateSurveyManager = () => { const [title, setTitle] = useState(''); - const [questions, setQuestions] = useState(defaultQuestions); - const [error, setError] = useState(''); + const [isCreating, setIsCreating] = useState(false); + const [isSubmitted, setIsSubmitted] = useState(false); + const router = useRouter(); + const { copy } = useCopyToClipboard(); + const { t } = useTranslation('surveyCreate'); const addQuestion = (newQuestion: Question) => { setQuestions((oldQuestions) => [...oldQuestions, newQuestion]); @@ -32,13 +35,6 @@ export const useCreateSurveyManager = () => { ); }; - const [isCreating, setIsCreating] = useState(false); - const [isSubmitted, setIsSubmitted] = useState(false); - - const router = useRouter(); - const { copy } = useCopyToClipboard(); - const { t } = useTranslation('surveyCreate'); - const handleChangeTitle = (e: React.ChangeEvent): void => { setError(''); setTitle(e.target.value);