diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 9d9214624..93df8045c 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -57,6 +57,7 @@ "playwright": "^1.43.1", "postcss": "^8.4.32", "prettier": "^3.1.1", + "resize-observer-polyfill": "^1.5.1", "stylus": "^0.63.0", "stylus-loader": "^8.0.0", "vite": "^4.1.4", @@ -5078,6 +5079,12 @@ "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==", "dev": true }, + "node_modules/resize-observer-polyfill": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", + "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==", + "dev": true + }, "node_modules/resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", diff --git a/frontend/package.json b/frontend/package.json index da72c2064..1f3e69547 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -64,6 +64,7 @@ "playwright": "^1.43.1", "postcss": "^8.4.32", "prettier": "^3.1.1", + "resize-observer-polyfill": "^1.5.1", "stylus": "^0.63.0", "stylus-loader": "^8.0.0", "vite": "^4.1.4", diff --git a/frontend/src/components/Dashboard.vue b/frontend/src/components/Dashboard.vue index 3915210fc..e7ef4da44 100644 --- a/frontend/src/components/Dashboard.vue +++ b/frontend/src/components/Dashboard.vue @@ -1,5 +1,4 @@ diff --git a/frontend/src/components/__tests__/Dashboard.spec.ts b/frontend/src/components/__tests__/Dashboard.spec.ts index ac3ccea0c..e6d25bbc5 100644 --- a/frontend/src/components/__tests__/Dashboard.spec.ts +++ b/frontend/src/components/__tests__/Dashboard.spec.ts @@ -1,11 +1,9 @@ import { expect, describe, it, vi, beforeEach } from 'vitest'; -import { render, waitFor, fireEvent } from '@testing-library/vue'; +import { render } from '@testing-library/vue'; import Dashboard from '../Dashboard.vue'; import { createTestingPinia } from '@pinia/testing'; import { authStore } from '../../store/modules/auth'; -import { DateTimeFormatter, LocalDate } from '@js-joda/core'; -import { Locale } from '@js-joda/locale_en'; const pinia = createTestingPinia(); const mockRouterPush = vi.fn(); @@ -13,6 +11,14 @@ const mockRouter = { push: (...args) => mockRouterPush(...args), }; +vi.mock('../../common/apiService', () => ({ + default: { + getReports: async () => { + return []; + }, + }, +})); + const wrappedRender = async () => { return render(Dashboard, { global: { @@ -24,17 +30,6 @@ const wrappedRender = async () => { }); }; -const mockGetReports = vi.fn(); -const mockGetReport = vi.fn(); -vi.mock('../../common/apiService', () => ({ - default: { - getReports: async () => { - return mockGetReports(); - }, - getReport: (...args) => mockGetReport(...args), - }, -})); - const auth = authStore(); describe('Dashboard', () => { @@ -44,72 +39,7 @@ describe('Dashboard', () => { }); it('should user name', async () => { - mockGetReports.mockReturnValue([]); const { getByTestId } = await wrappedRender(); expect(getByTestId('legal-name')).toHaveTextContent('Welcome, Test'); }); - - it('should display correct records', async () => { - mockGetReports.mockReturnValue([ - { - report_id: 'id1', - report_start_date: '2023-01-01', - report_end_date: '2023-02-01', - reporting_year: 2023, - create_date: new Date().toISOString(), - }, - ]); - const { getByTestId } = await wrappedRender(); - await waitFor(() => { - expect(mockGetReports).toHaveBeenCalled(); - }); - - expect(getByTestId('reporting_year-id1')).toHaveTextContent('2023'); - expect(getByTestId('report_published_date-id1')).toHaveTextContent( - LocalDate.now().format( - DateTimeFormatter.ofPattern('MMMM d, YYYY').withLocale(Locale.ENGLISH), - ), - ); - }); - it('should open report details', async () => { - mockGetReports.mockReturnValue([ - { - report_id: 'id1', - report_start_date: '2023-01-01', - report_end_date: '2023-02-01', - create_date: new Date().toISOString(), - }, - ]); - const { getByTestId } = await wrappedRender(); - await waitFor(() => { - expect(mockGetReports).toHaveBeenCalled(); - }); - - const viewReportButton = getByTestId('view-report-id1'); - await fireEvent.click(viewReportButton); - }); - it('should open report in edit mode', async () => { - mockGetReports.mockReturnValue([ - { - report_id: 'id1', - report_start_date: '2023-01-01', - report_end_date: '2023-02-01', - create_date: new Date().toISOString(), - is_unlocked: true, - }, - ]); - const { getByTestId } = await wrappedRender(); - await waitFor(() => { - expect(mockGetReports).toHaveBeenCalled(); - }); - - const editReportButton = getByTestId('edit-report-id1'); - await waitFor(() => { - expect(editReportButton).toBeInTheDocument(); - }); - await fireEvent.click(editReportButton); - expect(mockRouterPush).toHaveBeenCalledWith({ - path: 'generate-report-form', - }); - }); }); diff --git a/frontend/src/components/util/ReportsTable.vue b/frontend/src/components/util/ReportsTable.vue new file mode 100644 index 000000000..1fc0ca91d --- /dev/null +++ b/frontend/src/components/util/ReportsTable.vue @@ -0,0 +1,118 @@ + + + + + diff --git a/frontend/src/components/util/__tests__/ReportsTable.spec.ts b/frontend/src/components/util/__tests__/ReportsTable.spec.ts new file mode 100644 index 000000000..1f64dc75c --- /dev/null +++ b/frontend/src/components/util/__tests__/ReportsTable.spec.ts @@ -0,0 +1,113 @@ +import { expect, describe, it, vi, beforeEach } from 'vitest'; + +import { render, waitFor, fireEvent } from '@testing-library/vue'; +import { createTestingPinia } from '@pinia/testing'; +import { DateTimeFormatter, LocalDate } from '@js-joda/core'; +import { Locale } from '@js-joda/locale_en'; +import { createVuetify } from 'vuetify'; +import * as components from 'vuetify/components'; +import * as directives from 'vuetify/directives'; +import ReportsTable from '../ReportsTable.vue'; + +const vuetify = createVuetify({ + components, + directives, +}); + +const pinia = createTestingPinia(); +const mockRouterPush = vi.fn(); +vi.mock('vue-router', () => ({ + useRouter: () => ({ + push: mockRouterPush + }) +})) + +global.ResizeObserver = require('resize-observer-polyfill'); + +const wrappedRender = async () => { + return render(ReportsTable, { + global: { + plugins: [pinia, vuetify] + }, + }); +}; + +const mockGetReports = vi.fn(); +const mockGetReport = vi.fn(); +vi.mock('../../../common/apiService', () => ({ + default: { + getReports: async () => { + return mockGetReports(); + }, + getReport: (...args) => mockGetReport(...args), + }, +})); + +describe('ReportsTable', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + it('should display correct records', async () => { + mockGetReports.mockReturnValue([ + { + report_id: 'id1', + report_start_date: '2023-01-01', + report_end_date: '2023-02-01', + reporting_year: 2023, + create_date: new Date().toISOString(), + }, + ]); + const { getByTestId } = await wrappedRender(); + await waitFor(() => { + expect(mockGetReports).toHaveBeenCalled(); + }); + + expect(getByTestId('reporting_year-id1')).toHaveTextContent('2023'); + expect(getByTestId('report_published_date-id1')).toHaveTextContent( + LocalDate.now().format( + DateTimeFormatter.ofPattern('MMMM d, YYYY').withLocale(Locale.ENGLISH), + ), + ); + }); + it('should open report details', async () => { + mockGetReports.mockReturnValue([ + { + report_id: 'id1', + report_start_date: '2023-01-01', + report_end_date: '2023-02-01', + create_date: new Date().toISOString(), + }, + ]); + const { getByTestId } = await wrappedRender(); + await waitFor(() => { + expect(mockGetReports).toHaveBeenCalled(); + }); + + const viewReportButton = getByTestId('view-report-id1'); + await fireEvent.click(viewReportButton); + }); + it('should open report in edit mode', async () => { + mockGetReports.mockReturnValue([ + { + report_id: 'id1', + report_start_date: '2023-01-01', + report_end_date: '2023-02-01', + create_date: new Date().toISOString(), + is_unlocked: true, + }, + ]); + const { getByTestId } = await wrappedRender(); + await waitFor(() => { + expect(mockGetReports).toHaveBeenCalled(); + }); + + const editReportButton = getByTestId('edit-report-id1'); + await waitFor(() => { + expect(editReportButton).toBeInTheDocument(); + }); + await fireEvent.click(editReportButton); + expect(mockRouterPush).toHaveBeenCalledWith({ + path: 'generate-report-form', + }); + }); +});