From 38d0a7efba1cf3ca3583cf59186670c3a347165f Mon Sep 17 00:00:00 2001 From: Benjamin Guan Date: Mon, 10 Jun 2024 23:04:05 -0400 Subject: [PATCH 1/5] testcases for issue #733 that need debugging --- .vscode/settings.json | 2 + package.json | 2 +- .../ChapterEditor/ChapterContent.test.js | 49 +++++++++++++++++++ .../ChapterEditor/ChapterInfo.test.js | 29 +++++++++++ .../ChapterEditor/SubChapterItem.test.js | 30 ++++++++++++ .../INoteEditor/INoteChapter.test.js | 31 ++++++++++++ 6 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js create mode 100644 src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js create mode 100644 src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js create mode 100644 src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..7a73a41bf --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/package.json b/package.json index ca5fb7ad8..31f6bdbb3 100644 --- a/package.json +++ b/package.json @@ -204,7 +204,7 @@ "transformIgnorePatterns": [ "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$", "^.+\\.module\\.(css|sass|scss)$", - "node_modules\/(?!axios)" + "node_modules/(?!axios)" ], "modulePaths": [ "src" diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js new file mode 100644 index 000000000..efa4a6400 --- /dev/null +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js @@ -0,0 +1,49 @@ +import { render, screen } from '@testing-library/react'; +// TypeError: Cannot read properties of undefined (reading '__buildHTMLFromChapter') +// (src/screens/EPub/controllers/file-builders/EPubParser.js:6:46) +import { v4 as uuidv4 } from 'uuid'; +import ChapterContent from './ChapterContent'; + +const test_uuid = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'; + +jest.mock('uuid', () => { + return { + v4: jest.fn(() => test_uuid), + }; +}); + + +describe('ChapterContent Component', () => { + const mockDispatch = jest.fn(); + const mockOnInsert = jest.fn(); + const mockOnRemove = jest.fn(); + const mockOnTextChange = jest.fn(); + const mockOnImageChange = jest.fn(); + + const props = { + id: 'ch-content-12345-uuid-0', + key: 'ch-content-12345-uuid-12345', + content: 'Sample text content', + index: 0, + dispatch: mockDispatch, + onRemove: mockOnRemove, + onTextChange: mockOnTextChange, + onImageChange: mockOnImageChange, + onInsert: mockOnInsert, + }; + + it('should render text content correctly', () => { + render(); + + expect(screen.getByText('Sample text content')).toBeVisible(); + }); + + it('should render Tags components with correct keys', () => { + const { getAllByTestId } = render(); + + const tags = getAllByTestId('tag'); + tags.forEach((tag) => { + expect(tag).toHaveAttribute('key', `tag-${props.key}-${test_uuid}`); + }); + }); + }); diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js new file mode 100644 index 000000000..8b57d23cc --- /dev/null +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js @@ -0,0 +1,29 @@ +import { render, screen } from '@testing-library/react'; +import { v4 as uuidv4 } from 'uuid'; +import ChapterInfo from './ChapterInfo'; + +const test_uuid = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'; + +jest.mock('uuid', () => { + return { + v4: jest.fn(() => test_uuid), + }; +}); + +describe('ChapterInfo Component', () => { + const baseProps = { + chapter: { id: 'chapter-id-1', title: 'Sample Chapter', contents: ["content 1", "content 2"], condition: "condition"}, + currChIndex: 0, + dispatch: jest.fn() + }; + + it('should render ChapterContent components with correct keys', () => { + // Invariant Violation: Could not find "store" + render(); + + const contents = getAllByTestId('content'); + contents.forEach((content) => { + expect(content).toHaveAttribute('key', `ch-content-chapter-id-1-${test_uuid}`); + }); + }); +}); \ No newline at end of file diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js new file mode 100644 index 000000000..e095a0a8d --- /dev/null +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js @@ -0,0 +1,30 @@ +import { render, screen } from '@testing-library/react'; +import SubChapterItem from './SubChapterItem'; +import { v4 as uuidv4 } from 'uuid'; + +const test_uuid = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'; + +jest.mock('uuid', () => { + return { + v4: jest.fn(() => test_uuid), + }; +}); + +describe('SubChapterItem Component', () => { + const baseProps = { + subChapter: { title: "title", id: 'chapter-id-1', contents : ["content 1", "content 2"] }, + subChapterIndex: 0, + currChIndex: 0, + dispatch: jest.fn() + }; + + it('should render SubChapterItem components with correct keys', () => { + // Invariant Violation: Could not find "store" + render(); + + const contents = getAllByTestId('content'); + contents.forEach((content) => { + expect(content).toHaveAttribute('key', `sch-content-chapter-id-1-${test_uuid}}`); + }); + }); +}); \ No newline at end of file diff --git a/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js b/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js new file mode 100644 index 000000000..d18337f97 --- /dev/null +++ b/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js @@ -0,0 +1,31 @@ +import { render, screen } from '@testing-library/react'; +// TypeError: Cannot read properties of undefined (reading '__buildHTMLFromChapter') +// (src/screens/EPub/controllers/file-builders/EPubParser.js:6:46) +import { v4 as uuidv4 } from 'uuid'; +import INoteChapter from './INoteChapter'; + +const test_uuid = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'; + +jest.mock('uuid', () => { + return { + v4: jest.fn(() => test_uuid), + }; +}); + +describe('INoteChapter Component', () => { + const baseProps = { + chapter: {id: "chapter-id-1", title: "Sample title"}, + chIdx: 0, + images: [], + dispatch: jest.fn() + }; + + it('should render INoteChapter components with correct keys', () => { + render(); + + const contents = getAllByTestId('content'); + contents.forEach((content) => { + expect(content).toHaveAttribute('key', `ch-content-chapter-id-1-${test_uuid}}`); + }); + }); +}); \ No newline at end of file From 7ed8a9d2544fd5f699cbd935107da23984fd6dd0 Mon Sep 17 00:00:00 2001 From: Benjamin Guan Date: Wed, 12 Jun 2024 13:05:49 -0400 Subject: [PATCH 2/5] followed Harsh's comments for readability --- .../EditEPubChapter/ChapterEditor/ChapterContent.js | 10 ++++------ .../ChapterEditor/ChapterContent.test.js | 2 +- .../views/EditEPubChapter/ChapterEditor/ChapterInfo.js | 9 ++++----- .../EditEPubChapter/ChapterEditor/SubChapterItem.js | 9 ++++----- .../EPub/views/EditINote/INoteEditor/INoteChapter.js | 9 ++++----- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js index 54eb5b391..ea6f84d31 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js @@ -25,7 +25,7 @@ let Tags = ({data, handleDelete}) => { } return ( - + {data} - {tags.map((data) => { - const uuid = uuidv4(); - return ( - - )})} + {tags.map((data) => + () + )} diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js index efa4a6400..1cc0bb15e 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js @@ -41,7 +41,7 @@ describe('ChapterContent Component', () => { it('should render Tags components with correct keys', () => { const { getAllByTestId } = render(); - const tags = getAllByTestId('tag'); + const tags = getAllByTestId('ChapterContent-test-tag'); tags.forEach((tag) => { expect(tag).toHaveAttribute('key', `tag-${props.key}-${test_uuid}`); }); diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.js index 0083c6cb1..92d6aaf72 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.js @@ -60,12 +60,11 @@ function ChapterInfo({ chapter, currChIndex, dispatch }) { onSave={onSaveTitle} /> - {contents.map((content, index) => { - const uuid = uuidv4(); - return ( + {contents.map((content, index) => + ( - )})} + ))} diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.js index 601be88da..37ad16b4c 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.js @@ -67,12 +67,11 @@ function SubChapterItem({ onSave={onSaveTitle} /> - {contents.map((content, index) => { - const uuid = uuidv4(); - return ( + {contents.map((content, index) => + ( - )})} + ))} diff --git a/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.js b/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.js index bdf80e936..23aeb2da3 100644 --- a/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.js +++ b/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.js @@ -247,10 +247,9 @@ function INoteChapter ({ ) : (// If the chapter has elements, then iterate through all of them - chapter.contents.map((content, itemIdx) => { - const uuid = uuidv4(); - return ( - + chapter.contents.map((content, itemIdx) => + ( + {mergeChapterBtnElement(itemIdx)} {splitBtnElement(itemIdx)} @@ -306,7 +305,7 @@ function INoteChapter ({ {watchVideoElement(chapter.contents.length)} )} - )}))} + )))} {insertType !== null && ( From f5676cd0b3722b638c6999f6f0c2e8d393e6c32a Mon Sep 17 00:00:00 2001 From: Benjamin Guan Date: Wed, 12 Jun 2024 13:10:52 -0400 Subject: [PATCH 3/5] followed Harsh's comments for readability --- .../views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js | 4 ++-- .../EditEPubChapter/ChapterEditor/SubChapterItem.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js index 8b57d23cc..0e6e6535b 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js @@ -17,11 +17,11 @@ describe('ChapterInfo Component', () => { dispatch: jest.fn() }; - it('should render ChapterContent components with correct keys', () => { + it('should render ChapterInfo components with correct keys', () => { // Invariant Violation: Could not find "store" render(); - const contents = getAllByTestId('content'); + const contents = getAllByTestId('ChapterInfo-test-tag'); contents.forEach((content) => { expect(content).toHaveAttribute('key', `ch-content-chapter-id-1-${test_uuid}`); }); diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js index e095a0a8d..585f8b6be 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js @@ -1,6 +1,6 @@ import { render, screen } from '@testing-library/react'; -import SubChapterItem from './SubChapterItem'; import { v4 as uuidv4 } from 'uuid'; +import SubChapterItem from './SubChapterItem'; const test_uuid = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'; @@ -22,7 +22,7 @@ describe('SubChapterItem Component', () => { // Invariant Violation: Could not find "store" render(); - const contents = getAllByTestId('content'); + const contents = getAllByTestId('SubChapterItem-test-tag'); contents.forEach((content) => { expect(content).toHaveAttribute('key', `sch-content-chapter-id-1-${test_uuid}}`); }); From 963b50b202b06811fb81a78bc2a386e9ff9a2cc1 Mon Sep 17 00:00:00 2001 From: Benjamin Guan Date: Wed, 12 Jun 2024 14:12:11 -0400 Subject: [PATCH 4/5] Fix build error --- .../EditEPubChapter/ChapterEditor/ChapterContent.test.js | 4 ++-- .../views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js | 2 +- .../EditEPubChapter/ChapterEditor/SubChapterItem.test.js | 2 +- .../EPub/views/EditINote/INoteEditor/INoteChapter.test.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js index 1cc0bb15e..fb92d0cdc 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.test.js @@ -39,9 +39,9 @@ describe('ChapterContent Component', () => { }); it('should render Tags components with correct keys', () => { - const { getAllByTestId } = render(); + render(); - const tags = getAllByTestId('ChapterContent-test-tag'); + const tags = screen.getAllByTestId('ChapterContent-test-tag'); tags.forEach((tag) => { expect(tag).toHaveAttribute('key', `tag-${props.key}-${test_uuid}`); }); diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js index 0e6e6535b..f7aef4223 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.test.js @@ -21,7 +21,7 @@ describe('ChapterInfo Component', () => { // Invariant Violation: Could not find "store" render(); - const contents = getAllByTestId('ChapterInfo-test-tag'); + const contents = screen.getAllByTestId('ChapterInfo-test-tag'); contents.forEach((content) => { expect(content).toHaveAttribute('key', `ch-content-chapter-id-1-${test_uuid}`); }); diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js index 585f8b6be..d4ab2aa52 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/SubChapterItem.test.js @@ -22,7 +22,7 @@ describe('SubChapterItem Component', () => { // Invariant Violation: Could not find "store" render(); - const contents = getAllByTestId('SubChapterItem-test-tag'); + const contents = screen.getAllByTestId('SubChapterItem-test-tag'); contents.forEach((content) => { expect(content).toHaveAttribute('key', `sch-content-chapter-id-1-${test_uuid}}`); }); diff --git a/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js b/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js index d18337f97..ffdb48b5e 100644 --- a/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js +++ b/src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.test.js @@ -23,7 +23,7 @@ describe('INoteChapter Component', () => { it('should render INoteChapter components with correct keys', () => { render(); - const contents = getAllByTestId('content'); + const contents = screen.getAllByTestId('content'); contents.forEach((content) => { expect(content).toHaveAttribute('key', `ch-content-chapter-id-1-${test_uuid}}`); }); From dc21b643cab7268822d800ce30c5855432733f7a Mon Sep 17 00:00:00 2001 From: Benjamin Guan Date: Wed, 12 Jun 2024 14:16:51 -0400 Subject: [PATCH 5/5] linter error fix --- .../EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js index ea6f84d31..6d379189e 100644 --- a/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js +++ b/src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterContent.js @@ -104,7 +104,7 @@ const ChapterContent = ({ /> {tags.map((data) => - () + () )}