From 80cdd87707b233b7fbfc8cce20ad1338633ab930 Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Sat, 5 Oct 2024 14:27:02 +0800 Subject: [PATCH 1/4] Introduce assertIsBefore --- src/app/components/home.test.tsx | 13 ++++--------- src/app/components/quiz.test.tsx | 5 ++--- src/test_utils/assert/is_before.ts | 3 +++ 3 files changed, 9 insertions(+), 12 deletions(-) create mode 100644 src/test_utils/assert/is_before.ts diff --git a/src/app/components/home.test.tsx b/src/app/components/home.test.tsx index 1e2f71e..4d87bf8 100644 --- a/src/app/components/home.test.tsx +++ b/src/app/components/home.test.tsx @@ -6,6 +6,7 @@ import { render } from '@testing-library/react' import '@testing-library/jest-dom' import { HomePageUIService } from './home' import { LocalStorageQuestionSetRepo } from '../../repo/question_set' +import { assertIsBefore } from '../../test_utils/assert/is_before' describe('HomePage', () => { // Detail of testing of the navigation of this page should be in larger scope tests like e2e tests @@ -34,15 +35,9 @@ describe('HomePage', () => { const durian = getByText('durian') const orange = getByText('Orange') - expect(apple.compareDocumentPosition(banana)).toBe( - Node.DOCUMENT_POSITION_FOLLOWING, - ) - expect(banana.compareDocumentPosition(durian)).toBe( - Node.DOCUMENT_POSITION_FOLLOWING, - ) - expect(durian.compareDocumentPosition(orange)).toBe( - Node.DOCUMENT_POSITION_FOLLOWING, - ) + assertIsBefore(apple, banana) + assertIsBefore(banana, durian) + assertIsBefore(durian, orange) }) }) diff --git a/src/app/components/quiz.test.tsx b/src/app/components/quiz.test.tsx index 8eb7f21..b277b6c 100644 --- a/src/app/components/quiz.test.tsx +++ b/src/app/components/quiz.test.tsx @@ -7,6 +7,7 @@ import { QuestionSet, } from '../../model/question_set' import { LocalStorageQuestionSetRepo } from '../../repo/question_set' +import { assertIsBefore } from '../../test_utils/assert/is_before' describe('MultipleChoiceQuiz', () => { const presetCorrectChoiceMcBuilder = () => { @@ -257,9 +258,7 @@ describe('MultipleChoiceQuiz', () => { // Choices are swapped in the page const choice1 = getByLabelText('Question 1 Choice 1 (Correct)') const choice2 = getByLabelText('Question 1 Choice 2') - expect(choice2.compareDocumentPosition(choice1)).toBe( - Node.DOCUMENT_POSITION_FOLLOWING, - ) + assertIsBefore(choice2, choice1) }) }) diff --git a/src/test_utils/assert/is_before.ts b/src/test_utils/assert/is_before.ts new file mode 100644 index 0000000..a0a5581 --- /dev/null +++ b/src/test_utils/assert/is_before.ts @@ -0,0 +1,3 @@ +export function assertIsBefore(a: HTMLElement, b: HTMLElement) { + expect(a.compareDocumentPosition(b)).toBe(Node.DOCUMENT_POSITION_FOLLOWING) +} From 78ace1ddd9cbf9f8cfe7a749ac5debafeff51746 Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Sat, 5 Oct 2024 14:27:50 +0800 Subject: [PATCH 2/4] Specify .nvmrc --- .nvmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .nvmrc diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..6aab9b4 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v18.18.0 From 2b4241665343980da72c0fe1fc896f37254d616a Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Sat, 5 Oct 2024 14:32:36 +0800 Subject: [PATCH 3/4] Update the test --- src/app/components/quiz.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/components/quiz.test.tsx b/src/app/components/quiz.test.tsx index b277b6c..f092f5f 100644 --- a/src/app/components/quiz.test.tsx +++ b/src/app/components/quiz.test.tsx @@ -238,7 +238,7 @@ describe('MultipleChoiceQuiz', () => { ) }) - it('should display getCurrentPlayQuestions of questionSet if they are different from its original questions', async () => { + it('should display swapped questionSet', async () => { const questionSet = new QuestionSetBuilderForTest() .appendQuestion({ mc: new MultipleChoiceBuilder() @@ -252,7 +252,7 @@ describe('MultipleChoiceQuiz', () => { const { renderResult: { getByLabelText }, } = renderMultipleChoicePage({ - questionSet: questionSet.newSwappedChoicesQuestionSet(), + questionSet: questionSet.newSwappedChoicesQuestionSet(), // The swapped choices question set has different getCurrentPlayQuestions from the original one }) // Choices are swapped in the page From 4d9b1eab7633a770a366b24bdf58d70211c9094f Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Sat, 5 Oct 2024 14:45:05 +0800 Subject: [PATCH 4/4] Also update e2e test --- cypress/e2e/spec.cy.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cypress/e2e/spec.cy.js b/cypress/e2e/spec.cy.js index c1345d0..3730317 100644 --- a/cypress/e2e/spec.cy.js +++ b/cypress/e2e/spec.cy.js @@ -48,9 +48,7 @@ describe('End to end tests', () => { cy.findByText('Answer 1').then(($el1) => { cy.findByText('Answer 2').then(($el2) => { - expect($el2[0].compareDocumentPosition($el1[0])).to.eq( - Node.DOCUMENT_POSITION_FOLLOWING, - ) + assertIsBefore($el2[0], $el1[0]) }) }) }) @@ -103,6 +101,13 @@ function assertIsInHomePage(cy) { cy.contains('Add New Question Set') } +// TODO: Copy from the main codebase, can't figure out how to import it here yet +function assertIsBefore(aHtmlElement, bHtmlElement) { + expect(aHtmlElement.compareDocumentPosition(bHtmlElement)).to.eq( + Node.DOCUMENT_POSITION_FOLLOWING, + ) +} + /** * TODO: Currently not support more than 2 choices or more than 1 question. But no need for e2e testing currently */