From 158f3a1c29d609bd05e044f0514bd08f7d953344 Mon Sep 17 00:00:00 2001 From: Colin Rotherham Date: Fri, 31 Jan 2025 12:47:45 +0000 Subject: [PATCH] style: avoid calling expect conditionally --- .../date-picker/date-picker.spec.js | 32 ++++++++++++------- .../sortable-table/sortable-table.spec.js | 17 +++++----- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/moj/components/date-picker/date-picker.spec.js b/src/moj/components/date-picker/date-picker.spec.js index f2be84b0a..424bbc561 100644 --- a/src/moj/components/date-picker/date-picker.spec.js +++ b/src/moj/components/date-picker/date-picker.spec.js @@ -784,17 +784,21 @@ describe('button menu JS API', () => { new MOJFrontend.DatePicker(component, config).init() calendarButton = screen.getByRole('button', { name: 'Choose date' }) + await user.click(calendarButton) - for (let i = 1; i <= lastDayinMonth; i++) { + for (let i = 1; i <= minDay; i++) { const testId = dayjs().date(i).startOf('day').format('D/M/YYYY') const dayButton = screen.getByTestId(testId) - if (i <= minDay) { - expect(dayButton).toHaveAttribute('aria-disabled', 'true') - } else { - expect(dayButton).not.toHaveAttribute('aria-disabled') - } + expect(dayButton).toHaveAttribute('aria-disabled', 'true') + } + + for (let i = minDay + 1; i <= lastDayinMonth; i++) { + const testId = dayjs().date(i).startOf('day').format('D/M/YYYY') + const dayButton = screen.getByTestId(testId) + + expect(dayButton).not.toHaveAttribute('aria-disabled') } }) @@ -806,17 +810,21 @@ describe('button menu JS API', () => { new MOJFrontend.DatePicker(component, config).init() calendarButton = screen.getByRole('button', { name: 'Choose date' }) + await user.click(calendarButton) - for (let i = 1; i <= lastDayinMonth; i++) { + for (let i = 1; i <= maxDay; i++) { const testId = dayjs().date(i).startOf('day').format('D/M/YYYY') const dayButton = screen.getByTestId(testId) - if (i > maxDay) { - expect(dayButton).toHaveAttribute('aria-disabled', 'true') - } else { - expect(dayButton).not.toHaveAttribute('aria-disabled') - } + expect(dayButton).not.toHaveAttribute('aria-disabled') + } + + for (let i = maxDay + 1; i <= lastDayinMonth; i++) { + const testId = dayjs().date(i).startOf('day').format('D/M/YYYY') + const dayButton = screen.getByTestId(testId) + + expect(dayButton).toHaveAttribute('aria-disabled', 'true') } }) diff --git a/src/moj/components/sortable-table/sortable-table.spec.js b/src/moj/components/sortable-table/sortable-table.spec.js index ef35e9d69..d77ec589d 100644 --- a/src/moj/components/sortable-table/sortable-table.spec.js +++ b/src/moj/components/sortable-table/sortable-table.spec.js @@ -74,14 +74,15 @@ describe('sortable table', () => { }) test('initialises with buttons in headers', () => { - const headers = component.querySelectorAll('th') - headers.forEach((header) => { - if (header.getAttribute('aria-sort')) { - const button = header.querySelector('button') - expect(button).toBeInTheDocument() - expect(button).toHaveTextContent(header.textContent) - } - }) + const headers = Array.from(component.querySelectorAll('th')).filter( + (header) => header.getAttribute('aria-sort') + ) + + for (const header of headers) { + const button = header.querySelector('button') + expect(button).toBeInTheDocument() + expect(button).toHaveTextContent(header.textContent) + } }) test('creates status box for announcements', () => {