Skip to content

Commit

Permalink
review: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
soleksy-splunk committed Jun 10, 2024
1 parent 86e1f98 commit a895db5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 54 deletions.
9 changes: 9 additions & 0 deletions ui/jest.polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ Object.defineProperties(globalThis, {
Request: { value: Request },
Response: { value: Response },
});

Object.defineProperty(URL, 'createObjectURL', {
// needed for package import EnterpriseViewOnlyPreset from '@splunk/dashboard-presets/EnterpriseViewOnlyPreset'
writable: true,
value: jest.fn(),
});

HTMLCanvasElement.prototype.getContext = jest.fn();

// --- END of unnecessary polyfills
25 changes: 3 additions & 22 deletions ui/src/pages/Dashboard/stories/DashboardPage.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import type { Meta, StoryObj } from '@storybook/react';
import React from 'react';
import { http, HttpResponse } from 'msw';
import DashboardPage from '../DashboardPage';
import {
MOCK_OVERVIEW_DEFINITION_JSON,
MOCK_DATA_INGESTION_TAB_TAB_DEFINITION,
MOCK_ERROR_TAB_DEFINITION,
} from '../tests/mockData';

import { DASHBOARD_JSON_MOCKS } from '../tests/mockJs';

const meta = {
component: DashboardPage,
title: 'DashboardPage',
render: () => <DashboardPage />,
parameters: {
msw: {
handlers: [
http.get('/custom/panels_to_display.json', () =>
HttpResponse.json({ default: true, custom: false })
),
http.get('/custom/overview_definition.json', () =>
HttpResponse.json(MOCK_OVERVIEW_DEFINITION_JSON)
),
http.get('/custom/data_ingestion_tab_definition.json', () =>
HttpResponse.json(MOCK_DATA_INGESTION_TAB_TAB_DEFINITION)
),
http.get('/custom/errors_tab_definition.json', () =>
HttpResponse.json(MOCK_ERROR_TAB_DEFINITION)
),
http.post('/services/search/jobs', () => HttpResponse.error()),
http.get('/services/authentication/current-context', () => HttpResponse.error()),
],
handlers: DASHBOARD_JSON_MOCKS,
},
snapshots: {
width: 1200,
Expand Down
32 changes: 5 additions & 27 deletions ui/src/pages/Dashboard/tests/DashBoardPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import { http, HttpResponse, RequestHandler } from 'msw';

import './mockJs';
import DashboardPage from '../DashboardPage';
import { server } from '../../../mocks/server';
import {
MOCK_OVERVIEW_DEFINITION_JSON,
MOCK_DATA_INGESTION_TAB_TAB_DEFINITION,
MOCK_ERROR_TAB_DEFINITION,
} from './mockData';

import { DASHBOARD_JSON_MOCKS } from './mockJs';

it('dashboard page renders waiting spinner', async () => {
server.use(http.get('/custom/panels_to_display.json', () => HttpResponse.json({})));
Expand All @@ -21,33 +17,15 @@ it('dashboard page renders waiting spinner', async () => {
});

it('render with all default dashboards', async () => {
server.use(
http.get('/custom/panels_to_display.json', () =>
HttpResponse.json({ default: true, custom: false })
),
http.get('/custom/overview_definition.json', () =>
HttpResponse.json(MOCK_OVERVIEW_DEFINITION_JSON)
),
http.get('/custom/data_ingestion_tab_definition.json', () =>
HttpResponse.json(MOCK_DATA_INGESTION_TAB_TAB_DEFINITION)
),
http.get('/custom/errors_tab_definition.json', () =>
HttpResponse.json(MOCK_ERROR_TAB_DEFINITION)
),
http.post('/services/search/jobs', () => HttpResponse.error()),
http.get('/services/authentication/current-context', () => HttpResponse.error())
);

DASHBOARD_JSON_MOCKS.forEach((mock: RequestHandler) => server.use(mock));
render(<DashboardPage />);
const timeLabels = await screen.findAllByText('Time');
expect(timeLabels[0]).toBeInTheDocument();
expect(timeLabels.length).toEqual(2);

const dataIngestionHeader = screen.getByText('Data ingestion');
expect(dataIngestionHeader).toBeInTheDocument();

const overviewInput = document.querySelector('[data-input-id="overview_input"]');
expect(overviewInput).toBeInTheDocument();

const idsToBeInDocument = [
'dashboardTable',
'data_ingestion_search',
Expand Down
27 changes: 22 additions & 5 deletions ui/src/pages/Dashboard/tests/mockJs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
Object.defineProperty(URL, 'createObjectURL', {
writable: true,
value: jest.fn(),
});
import { http, HttpResponse } from 'msw';
import {
MOCK_OVERVIEW_DEFINITION_JSON,
MOCK_DATA_INGESTION_TAB_TAB_DEFINITION,
MOCK_ERROR_TAB_DEFINITION,
} from './mockData';

// needed for package import EnterpriseViewOnlyPreset from '@splunk/dashboard-presets/EnterpriseViewOnlyPreset'
export const DASHBOARD_JSON_MOCKS = [
http.get('/custom/panels_to_display.json', () =>
HttpResponse.json({ default: true, custom: false })
),
http.get('/custom/overview_definition.json', () =>
HttpResponse.json(MOCK_OVERVIEW_DEFINITION_JSON)
),
http.get('/custom/data_ingestion_tab_definition.json', () =>
HttpResponse.json(MOCK_DATA_INGESTION_TAB_TAB_DEFINITION)
),
http.get('/custom/errors_tab_definition.json', () =>
HttpResponse.json(MOCK_ERROR_TAB_DEFINITION)
),
http.post('/services/search/jobs', () => HttpResponse.error()),
http.get('/services/authentication/current-context', () => HttpResponse.error()),
];

0 comments on commit a895db5

Please sign in to comment.