Skip to content

Commit

Permalink
test: add tests for validateReviewFields
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh committed Mar 8, 2024
1 parent 22a6136 commit 5c39694
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 9 deletions.
159 changes: 159 additions & 0 deletions ui/src/lib/utils/validate-review-fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import {
$getRoot,
$createParagraphNode,
$createTextNode,
type LexicalEditor,
type EditorState,
} from 'lexical';

import { createHeadlessEditor } from '@lexical/headless';

import { validateReviewFields } from '@/lib/utils/validate-review-fields';

describe('validateReviewFields', () => {
let editor: LexicalEditor;
let rawData: {
title: string;
movieName: string;
editorState: EditorState;
};

async function update(updateFn: () => void) {
editor.update(updateFn);
await Promise.resolve();
}

beforeEach(() => {
editor = createHeadlessEditor({
onError: (error) => {
throw error;
},
});
});

test('should return proper CreateReviewReqeust on success ', async () => {
await update(() => {
$getRoot().append($createParagraphNode().append($createTextNode('hi')));
});

rawData = {
title: 'foo',
movieName: 'bar',
editorState: editor.getEditorState(),
};

expect(validateReviewFields(rawData)).toEqual({
success: true,
data: {
title: 'foo',
movieName: 'bar',
content:
'{"description":"hi","serializedEditorState":{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"hi","type":"text","version":1}],"direction":null,"format":"","indent":0,"type":"paragraph","version":1}],"direction":null,"format":"","indent":0,"type":"root","version":1}}}',
},
});
});

describe('on fail', () => {
test('should return error message for empty title', async () => {
await update(() => {
$getRoot().append($createParagraphNode().append($createTextNode('hi')));
});

rawData = {
title: '',
movieName: 'bar',
editorState: editor.getEditorState(),
};

expect(validateReviewFields(rawData)).toEqual({
success: false,
errors: {
title: '제목을 입력해주세요.',
},
});
});

test('should return error message for empty movieTitme', async () => {
await update(() => {
$getRoot().append($createParagraphNode().append($createTextNode('hi')));
});

rawData = {
title: 'foo',
movieName: '',
editorState: editor.getEditorState(),
};

expect(validateReviewFields(rawData)).toEqual({
success: false,
errors: {
movieName: '영화 제목을 입력해주세요.',
},
});
});

test('should return error message for empty content', () => {
rawData = {
title: 'foo',
movieName: 'bar',
editorState: editor.getEditorState(),
};

expect(validateReviewFields(rawData)).toEqual({
success: false,
errors: {
content: '내용을 입력해주세요.',
},
});
});

test('should return error message for invalid EditorState', () => {
rawData = {
title: 'foo',
movieName: 'bar',
editorState: { root: null } as unknown as EditorState,
};

expect(validateReviewFields(rawData)).toEqual({
success: false,
errors: {
content: '내용을 저장하는 중 오류가 발생했습니다.',
},
});
});

test('should return error messages for several fields', async () => {
rawData = {
title: 'foo',
movieName: '',
editorState: { root: null } as unknown as EditorState,
};

expect(validateReviewFields(rawData)).toEqual({
success: false,
errors: {
movieName: '영화 제목을 입력해주세요.',
content: '내용을 저장하는 중 오류가 발생했습니다.',
},
});

await update(() => {
$getRoot().append($createParagraphNode().append($createTextNode('hi')));
});

rawData = {
title: '',
movieName: '',
editorState: editor.getEditorState(),
};

expect(validateReviewFields(rawData)).toEqual({
success: false,
errors: {
title: '제목을 입력해주세요.',
movieName: '영화 제목을 입력해주세요.',
},
});
});
});
});
13 changes: 4 additions & 9 deletions ui/src/lib/utils/validate-review-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ export function validateReviewFields(rawData: RawReviewField): OnSuccess | OnFai
const { title, movieName, editorState } = rawData;

if (title.length < 1) {
errors.set('title', 'title is empty');
errors.set('title', '제목을 입력해주세요.');
}

// TODO: validate max length after dicussion
// if (title.length > 20) {
// errors.set('title', 'title is too long. it should be less than 20');
// }

if (movieName.length < 1) {
errors.set('movieName', 'movieName is empty');
errors.set('movieName', '영화 제목을 입력해주세요.');
}

let reviewContent: {
Expand All @@ -46,13 +41,13 @@ export function validateReviewFields(rawData: RawReviewField): OnSuccess | OnFai

try {
if (editorState.read($isRootTextContentEmptyCurry(false))) {
errors.set('content', 'content is empty');
errors.set('content', '내용을 입력해주세요.');
}

reviewContent = getReviewContent(editorState);
serializedContent = JSON.stringify(reviewContent);
} catch (error) {
errors.set('content', 'content has something wrong');
errors.set('content', `내용을 저장하는 중 오류가 발생했습니다.`);
}

if (errors.size > 0) {
Expand Down

0 comments on commit 5c39694

Please sign in to comment.