Skip to content

Commit

Permalink
Tests/Add create survey unit tests (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryczko authored Aug 9, 2023
1 parent 4d8321c commit cfe5338
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 13 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
"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/",
"lint-fix": "eslint --ext .js,.ts,.tsx src/ --fix",
"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",
Expand Down Expand Up @@ -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",
Expand Down
115 changes: 115 additions & 0 deletions src/features/surveys/managers/createSurveyManager.test.ts
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
);
});
});
});
14 changes: 5 additions & 9 deletions src/features/surveys/managers/createSurveyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ export interface Question {

export const useCreateSurveyManager = () => {
const [title, setTitle] = useState('');

const [questions, setQuestions] = useState<Question[]>(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]);
Expand All @@ -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<HTMLInputElement>): void => {
setError('');
setTitle(e.target.value);
Expand Down

1 comment on commit cfe5338

@vercel
Copy link

@vercel vercel bot commented on cfe5338 Aug 13, 2023

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

Please sign in to comment.