From 765443ebc27109de7c0e5c5ce1795be922045d47 Mon Sep 17 00:00:00 2001 From: Christoph Lienhard Date: Tue, 30 Mar 2021 17:57:28 +0200 Subject: [PATCH] Refactor: Fix eslint problems in tests --- .../answer-list.integration.test.tsx | 210 +++++++++--------- .../question-list.integration.test.tsx | 68 +++--- 2 files changed, 139 insertions(+), 139 deletions(-) diff --git a/redaktions-app/src/integration-tests/answer-list.integration.test.tsx b/redaktions-app/src/integration-tests/answer-list.integration.test.tsx index bf0035d..cb51f77 100644 --- a/redaktions-app/src/integration-tests/answer-list.integration.test.tsx +++ b/redaktions-app/src/integration-tests/answer-list.integration.test.tsx @@ -28,6 +28,111 @@ import { } from "./test-helper"; import QuestionAnswersList from "../components/QuestionAnswerList"; +function renderQuestionAnswerList(additionalMocks?: Array) { + const initialMocks = [ + ...getAllQuestionAnswersMock, + ...getAnswerByQuestionAndPersonMock, + ]; + const allMocks = additionalMocks + ? [...initialMocks, ...additionalMocks] + : initialMocks; + return render( + + + + + + + + ); +} + +const waitForQuestionsToRender = async (): Promise> => { + const numberOfAnswersInMockQuery = questionAnswersMock.length; + let questionAnswerCards: Array = []; + await waitFor(() => { + questionAnswerCards = screen.queryAllByRole("button", { + name: /Question [1-3]/, + }); + expect(questionAnswerCards.length).toEqual(numberOfAnswersInMockQuery); + }); + return questionAnswerCards; +}; + +const getSaveAnswerButton = (parent: HTMLElement) => { + return findByRole(parent, "button", { name: /speichern/i }); +}; + +const getResetAnswerButton = (parent: HTMLElement) => { + return findByRole(parent, "button", { name: /zurücksetzen/i }); +}; + +interface PositionIconButtons { + positive: HTMLElement; + neutral: HTMLElement; + negative: HTMLElement; + skipped: HTMLElement; +} + +const getCandidatePositionButtons = async ( + parent: HTMLElement +): Promise => { + const labelAboveButtons = await findByText(parent, /Deine Position/i); + const parentElement = labelAboveButtons.parentElement as HTMLElement; + const positive = queryAllIconButtons( + getPositivePositionPath(), + parentElement + )[0]; + const neutral = queryAllIconButtons( + getNeutralPositionPath(), + parentElement + )[0]; + const negative = queryAllIconButtons( + getNegativePositionPath(), + parentElement + )[0]; + const skipped = queryAllIconButtons( + getSkippedPositionPath(), + parentElement + )[0]; + expect(positive).toBeDefined(); + expect(neutral).toBeDefined(); + expect(negative).toBeDefined(); + expect(skipped).toBeDefined(); + return { + positive, + neutral, + negative, + skipped, + }; +}; + +interface AccordionAnswerSection { + positionIconButtons: PositionIconButtons; + textField: HTMLElement; + saveButton: HTMLElement; + resetButton: HTMLElement; +} + +const expandAccordionAndGetAnswerSection = async ( + accordionExpandArea: HTMLElement +): Promise => { + fireEvent.click(accordionExpandArea); + let fullAccordion = accordionExpandArea.parentElement; + expect(fullAccordion).not.toBeNull(); + fullAccordion = fullAccordion as HTMLElement; + const textField = await findByRole(fullAccordion, "textbox"); + const saveButton = await getSaveAnswerButton(fullAccordion); + const resetButton = await getResetAnswerButton(fullAccordion); + const positionIconButtons = await getCandidatePositionButtons(fullAccordion); + return { + positionIconButtons, + textField, + saveButton, + resetButton, + }; +}; + describe("The AnswerList", () => { test("displays the existing answers, but not the details of it", async () => { renderQuestionAnswerList(); @@ -145,108 +250,3 @@ describe("The AnswerList", () => { ); }); }); - -function renderQuestionAnswerList(additionalMocks?: Array) { - const initialMocks = [ - ...getAllQuestionAnswersMock, - ...getAnswerByQuestionAndPersonMock, - ]; - const allMocks = additionalMocks - ? [...initialMocks, ...additionalMocks] - : initialMocks; - return render( - - - - - - - - ); -} - -const waitForQuestionsToRender = async (): Promise> => { - const numberOfAnswersInMockQuery = questionAnswersMock.length; - let questionAnswerCards: Array = []; - await waitFor(() => { - questionAnswerCards = screen.queryAllByRole("button", { - name: /Question [1-3]/, - }); - expect(questionAnswerCards.length).toEqual(numberOfAnswersInMockQuery); - }); - return questionAnswerCards; -}; - -const getSaveAnswerButton = (parent: HTMLElement) => { - return findByRole(parent, "button", { name: /speichern/i }); -}; - -const getResetAnswerButton = (parent: HTMLElement) => { - return findByRole(parent, "button", { name: /zurücksetzen/i }); -}; - -interface PositionIconButtons { - positive: HTMLElement; - neutral: HTMLElement; - negative: HTMLElement; - skipped: HTMLElement; -} - -const getCandidatePositionButtons = async ( - parent: HTMLElement -): Promise => { - const labelAboveButtons = await findByText(parent, /Deine Position/i); - const parentElement = labelAboveButtons.parentElement as HTMLElement; - const positive = queryAllIconButtons( - getPositivePositionPath(), - parentElement - )[0]; - const neutral = queryAllIconButtons( - getNeutralPositionPath(), - parentElement - )[0]; - const negative = queryAllIconButtons( - getNegativePositionPath(), - parentElement - )[0]; - const skipped = queryAllIconButtons( - getSkippedPositionPath(), - parentElement - )[0]; - expect(positive).toBeDefined(); - expect(neutral).toBeDefined(); - expect(negative).toBeDefined(); - expect(skipped).toBeDefined(); - return { - positive, - neutral, - negative, - skipped, - }; -}; - -interface AccordionAnswerSection { - positionIconButtons: PositionIconButtons; - textField: HTMLElement; - saveButton: HTMLElement; - resetButton: HTMLElement; -} - -const expandAccordionAndGetAnswerSection = async ( - accordionExpandArea: HTMLElement -): Promise => { - fireEvent.click(accordionExpandArea); - let fullAccordion = accordionExpandArea.parentElement; - expect(fullAccordion).not.toBeNull(); - fullAccordion = fullAccordion as HTMLElement; - const textField = await findByRole(fullAccordion, "textbox"); - const saveButton = await getSaveAnswerButton(fullAccordion); - const resetButton = await getResetAnswerButton(fullAccordion); - const positionIconButtons = await getCandidatePositionButtons(fullAccordion); - return { - positionIconButtons, - textField, - saveButton, - resetButton, - }; -}; diff --git a/redaktions-app/src/integration-tests/question-list.integration.test.tsx b/redaktions-app/src/integration-tests/question-list.integration.test.tsx index 954c9f1..8741256 100644 --- a/redaktions-app/src/integration-tests/question-list.integration.test.tsx +++ b/redaktions-app/src/integration-tests/question-list.integration.test.tsx @@ -21,6 +21,40 @@ import { queryAllEditIconButtons, } from "./test-helper"; +function renderQuestionList(additionalMocks?: Array) { + const initialMocks = [ + ...getAllQuestionsMock, + ...getQuestionByIdMock, + ...getAllCategoriesMock, + ]; + const allMocks = additionalMocks + ? [...initialMocks, ...additionalMocks] + : initialMocks; + return render( + + + + + + + + ); +} + +const waitForInitialQuestionsToRender = async (): Promise< + Array +> => { + const numberOfQuestionsInMockQuery = questionNodesMock.length; + let questionCards: Array = []; + await waitFor(() => { + questionCards = screen.queryAllByRole("button", { + name: /Question [1-3]\?/, + }); + expect(questionCards.length).toEqual(numberOfQuestionsInMockQuery); + }); + return questionCards; +}; + describe("The QuestionList", () => { test("displays the existing questions, but not the details of it", async () => { renderQuestionList(); @@ -138,37 +172,3 @@ describe("The QuestionList", () => { }); }); }); - -function renderQuestionList(additionalMocks?: Array) { - const initialMocks = [ - ...getAllQuestionsMock, - ...getQuestionByIdMock, - ...getAllCategoriesMock, - ]; - const allMocks = additionalMocks - ? [...initialMocks, ...additionalMocks] - : initialMocks; - return render( - - - - - - - - ); -} - -const waitForInitialQuestionsToRender = async (): Promise< - Array -> => { - const numberOfQuestionsInMockQuery = questionNodesMock.length; - let questionCards: Array = []; - await waitFor(() => { - questionCards = screen.queryAllByRole("button", { - name: /Question [1-3]\?/, - }); - expect(questionCards.length).toEqual(numberOfQuestionsInMockQuery); - }); - return questionCards; -};