From 7f33886240ed521363c09279ab0fc8afdaced39a Mon Sep 17 00:00:00 2001 From: Remi Date: Fri, 5 Feb 2021 22:52:08 +0100 Subject: [PATCH] tests(splunk-on-call-plugin): update tests --- plugins/splunk-on-call/src/api/mocks.ts | 83 +++++++++++++++++++ .../components/Escalation/Escalation.test.tsx | 26 +----- .../components/Incident/Incidents.test.tsx | 60 ++++---------- .../src/components/SplunkOnCallCard.test.tsx | 22 ++--- .../TriggerDialog/TriggerDialog.test.tsx | 36 +++----- 5 files changed, 119 insertions(+), 108 deletions(-) create mode 100644 plugins/splunk-on-call/src/api/mocks.ts diff --git a/plugins/splunk-on-call/src/api/mocks.ts b/plugins/splunk-on-call/src/api/mocks.ts new file mode 100644 index 0000000000..858e255d18 --- /dev/null +++ b/plugins/splunk-on-call/src/api/mocks.ts @@ -0,0 +1,83 @@ +import { Incident, Team, User } from '../components/types'; + +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export const MOCKED_USER: User = { + createdAt: '2021-02-01T23:38:38Z', + displayName: 'Test User', + email: 'test@example.com', + firstName: 'FirstNameTest', + lastName: 'LastNameTest', + passwordLastUpdated: '2021-02-01T23:38:38Z', + username: 'test_user', + verified: true, + _selfUrl: '/api-public/v1/user/test_user', +}; + +export const MOCKED_ON_CALL = [ + { + team: { name: 'team_example', slug: 'team-zEalMCgwYSA0Lt40' }, + oncallNow: [ + { + escalationPolicy: { name: 'Example', slug: 'team-zEalMCgwYSA0Lt40' }, + users: [{ onCalluser: { username: 'test_user' } }], + }, + ], + }, +]; + +export const MOCK_INCIDENT: Incident = { + alertCount: 1, + currentPhase: 'ACKED', + entityDisplayName: 'test-incident', + entityId: 'entityId', + entityState: 'CRITICAL', + entityType: 'SERVICE', + incidentNumber: '1', + lastAlertId: 'lastAlertId', + lastAlertTime: '2021-02-03T00:13:11Z', + routingKey: 'routingdefault', + service: 'test', + startTime: '2021-02-03T00:13:11Z', + pagedTeams: ['team-O9SqT13fsnCstjMi'], + pagedUsers: [], + pagedPolicies: [ + { + policy: { + name: 'Generated Direct User Policy for test', + slug: 'directUserPolicySlug-test', + _selfUrl: '/test', + }, + }, + ], + transitions: [{ name: 'ACKED', at: '2021-02-03T01:20:00Z', by: 'test' }], + monitorName: 'vouser-user', + monitorType: 'Manual', + firstAlertUuid: 'firstAlertUuid', + incidentLink: 'https://portal.victorops.com/example', +}; + +export const MOCK_TEAM: Team = { + _selfUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi', + _membersUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi/members', + _policiesUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi/policies', + _adminsUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi/admins', + name: 'test', + slug: 'team-O9SqT13fsnCstjMi', + memberCount: 1, + version: 1, + isDefaultTeam: false, +}; diff --git a/plugins/splunk-on-call/src/components/Escalation/Escalation.test.tsx b/plugins/splunk-on-call/src/components/Escalation/Escalation.test.tsx index 305d29b8b8..41157a2b5e 100644 --- a/plugins/splunk-on-call/src/components/Escalation/Escalation.test.tsx +++ b/plugins/splunk-on-call/src/components/Escalation/Escalation.test.tsx @@ -17,33 +17,9 @@ import React from 'react'; import { render, waitFor } from '@testing-library/react'; import { EscalationPolicy } from './EscalationPolicy'; import { wrapInTestApp } from '@backstage/test-utils'; -import { User } from '../types'; import { ApiProvider, ApiRegistry } from '@backstage/core'; import { splunkOnCallApiRef } from '../../api'; - -const MOCKED_USER: User = { - createdAt: '2021-02-01T23:38:38Z', - displayName: 'Test User', - email: 'test@example.com', - firstName: 'FirstNameTest', - lastName: 'LastNameTest', - passwordLastUpdated: '2021-02-01T23:38:38Z', - username: 'test_user', - verified: true, - _selfUrl: '/api-public/v1/user/test_user', -}; - -const MOCKED_ON_CALL = [ - { - team: { name: 'team_example', slug: 'team-zEalMCgwYSA0Lt40' }, - oncallNow: [ - { - escalationPolicy: { name: 'Example', slug: 'team-zEalMCgwYSA0Lt40' }, - users: [{ onCalluser: { username: 'test_user' } }], - }, - ], - }, -]; +import { MOCKED_ON_CALL, MOCKED_USER } from '../../api/mocks'; const mockSplunkOnCallApi = { getOnCallUsers: () => [], diff --git a/plugins/splunk-on-call/src/components/Incident/Incidents.test.tsx b/plugins/splunk-on-call/src/components/Incident/Incidents.test.tsx index 51bbe50aef..8ca546c31e 100644 --- a/plugins/splunk-on-call/src/components/Incident/Incidents.test.tsx +++ b/plugins/splunk-on-call/src/components/Incident/Incidents.test.tsx @@ -18,56 +18,15 @@ import { render, waitFor } from '@testing-library/react'; import { Incidents } from './Incidents'; import { wrapInTestApp } from '@backstage/test-utils'; import { + alertApiRef, ApiProvider, ApiRegistry, + createApiRef, IdentityApi, identityApiRef, } from '@backstage/core'; import { splunkOnCallApiRef } from '../../api'; -import { Incident, Team } from '../types'; - -const MOCK_INCIDENT: Incident = { - alertCount: 1, - currentPhase: 'ACKED', - entityDisplayName: 'test-incident', - entityId: 'entityId', - entityState: 'CRITICAL', - entityType: 'SERVICE', - incidentNumber: '1', - lastAlertId: 'lastAlertId', - lastAlertTime: '2021-02-03T00:13:11Z', - routingKey: 'routingdefault', - service: 'test', - startTime: '2021-02-03T00:13:11Z', - pagedTeams: ['team-O9SqT13fsnCstjMi'], - pagedUsers: [], - pagedPolicies: [ - { - policy: { - name: 'Generated Direct User Policy for test', - slug: 'directUserPolicySlug-test', - _selfUrl: '/test', - }, - }, - ], - transitions: [{ name: 'ACKED', at: '2021-02-03T01:20:00Z', by: 'test' }], - monitorName: 'vouser-user', - monitorType: 'Manual', - firstAlertUuid: 'firstAlertUuid', - incidentLink: 'https://portal.victorops.com/example', -}; - -const MOCK_TEAM: Team = { - _selfUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi', - _membersUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi/members', - _policiesUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi/policies', - _adminsUrl: '/api-public/v1/team/team-O9SqT13fsnCstjMi/admins', - name: 'test', - slug: 'team-O9SqT13fsnCstjMi', - memberCount: 1, - version: 1, - isDefaultTeam: false, -}; +import { MOCK_TEAM, MOCK_INCIDENT } from '../../api/mocks'; const mockIdentityApi: Partial = { getUserId: () => 'test', @@ -78,6 +37,13 @@ const mockSplunkOnCallApi = { getTeams: () => [], }; const apis = ApiRegistry.from([ + [ + alertApiRef, + createApiRef({ + id: 'core.alert', + description: 'Used to report alerts and forward them to the app', + }), + ], [identityApiRef, mockIdentityApi], [splunkOnCallApiRef, mockSplunkOnCallApi], ]); @@ -121,7 +87,11 @@ describe('Incidents', () => { ), ); await waitFor(() => !queryByTestId('progress')); - expect(getByText('user')).toBeInTheDocument(); + expect( + getByText('user', { + exact: false, + }), + ).toBeInTheDocument(); expect(getByText('test-incident')).toBeInTheDocument(); expect(getByTitle('ACKED')).toBeInTheDocument(); expect(getByLabelText('Status warning')).toBeInTheDocument(); diff --git a/plugins/splunk-on-call/src/components/SplunkOnCallCard.test.tsx b/plugins/splunk-on-call/src/components/SplunkOnCallCard.test.tsx index 9ca61614a4..7d4709ecb9 100644 --- a/plugins/splunk-on-call/src/components/SplunkOnCallCard.test.tsx +++ b/plugins/splunk-on-call/src/components/SplunkOnCallCard.test.tsx @@ -29,22 +29,18 @@ import { UnauthorizedError, SplunkOnCallClient, } from '../api'; -import { User } from './types'; - -const MOCKED_USER: User = { - createdAt: '2021-02-01T23:38:38Z', - displayName: 'Test User', - email: 'remi.d45@gmail.com', - firstName: 'FirstNameTest', - lastName: 'LastNameTest', - passwordLastUpdated: '2021-02-01T23:38:38Z', - username: 'test_user', - verified: true, - _selfUrl: '/api-public/v1/user/test_user', -}; +import { + MOCKED_ON_CALL, + MOCKED_USER, + MOCK_INCIDENT, + MOCK_TEAM, +} from '../api/mocks'; const mockSplunkOnCallApi: Partial = { getUsers: async () => [], + getIncidents: async () => [MOCK_INCIDENT], + getOnCallUsers: async () => MOCKED_ON_CALL, + getTeams: async () => [MOCK_TEAM], }; const apis = ApiRegistry.from([ diff --git a/plugins/splunk-on-call/src/components/TriggerDialog/TriggerDialog.test.tsx b/plugins/splunk-on-call/src/components/TriggerDialog/TriggerDialog.test.tsx index 8ffe55fe01..16fbe815d0 100644 --- a/plugins/splunk-on-call/src/components/TriggerDialog/TriggerDialog.test.tsx +++ b/plugins/splunk-on-call/src/components/TriggerDialog/TriggerDialog.test.tsx @@ -26,19 +26,7 @@ import { } from '@backstage/core'; import { splunkOnCallApiRef } from '../../api'; import { TriggerDialog } from './TriggerDialog'; -import { User } from '../types'; - -const MOCKED_USER: User = { - createdAt: '2021-02-01T23:38:38Z', - displayName: 'Test User', - email: 'remi.d45@gmail.com', - firstName: 'FirstNameTest', - lastName: 'LastNameTest', - passwordLastUpdated: '2021-02-01T23:38:38Z', - username: 'test_user', - verified: true, - _selfUrl: '/api-public/v1/user/test_user', -}; +import { MOCKED_USER } from '../../api/mocks'; describe('TriggerDialog', () => { const mockIdentityApi: Partial = { @@ -48,18 +36,16 @@ describe('TriggerDialog', () => { const mockTriggerAlarmFn = jest.fn(); const mockSplunkOnCallApi = { triggerAlarm: mockTriggerAlarmFn, - getEscalationPolicies: async () => ({ - policies: [ - { - policy: { - name: 'Example', - slug: 'team-zEalMCgwYSA0Lt40', - _selfUrl: '/api-public/v1/policies/team-zEalMCgwYSA0Lt40', - }, - team: { name: 'Example', slug: 'team-zEalMCgwYSA0Lt40' }, + getEscalationPolicies: async () => [ + { + policy: { + name: 'Example', + slug: 'team-zEalMCgwYSA0Lt40', + _selfUrl: '/api-public/v1/policies/team-zEalMCgwYSA0Lt40', }, - ], - }), + team: { name: 'Example', slug: 'team-zEalMCgwYSA0Lt40' }, + }, + ], }; const apis = ApiRegistry.from([ @@ -103,8 +89,8 @@ describe('TriggerDialog', () => { await act(async () => { fireEvent.change(summary, { target: { value: description } }); fireEvent.change(body, { target: { value: description } }); - fireEvent.change(userTarget, { target: { value: ['test_user'] } }); fireEvent.change(behavior, { target: { value: '0' } }); + fireEvent.change(userTarget, { target: { value: ['test_user'] } }); fireEvent.change(policiesTarget, { target: { value: ['team-zEalMCgwYSA0Lt40'] }, });