From 0225aef248c18e61246b1f50e4ec189e8192511a Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Thu, 20 Dec 2018 10:54:16 +0100 Subject: [PATCH] Provide new test helper functions for creating mocks --- .../overview/meta/__tests__/MetaTags-test.tsx | 22 +++----- .../src/main/js/helpers/testUtils.ts | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaTags-test.tsx b/server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaTags-test.tsx index b2ed2b1ac1e..a9bbe419265 100644 --- a/server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaTags-test.tsx +++ b/server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaTags-test.tsx @@ -20,30 +20,22 @@ import * as React from 'react'; import { shallow } from 'enzyme'; import MetaTags from '../MetaTags'; +import { mockComponent } from '../../../../helpers/testUtils'; -const component = { - key: 'my-project', - tags: [], +const component = mockComponent({ configuration: { showSettings: false - }, - organization: 'foo', - qualifier: 'TRK', - name: 'MyProject', - breadcrumbs: [] -}; + } +}); -const componentWithTags = { +const componentWithTags = mockComponent({ key: 'my-second-project', tags: ['foo', 'bar'], configuration: { showSettings: true }, - organization: 'foo', - qualifier: 'TRK', - name: 'MySecondProject', - breadcrumbs: [] -}; + name: 'MySecondProject' +}); it('should render without tags and admin rights', () => { expect( diff --git a/server/sonar-web/src/main/js/helpers/testUtils.ts b/server/sonar-web/src/main/js/helpers/testUtils.ts index 399363f6418..429d60e4d14 100644 --- a/server/sonar-web/src/main/js/helpers/testUtils.ts +++ b/server/sonar-web/src/main/js/helpers/testUtils.ts @@ -19,6 +19,7 @@ */ import { ShallowWrapper, ReactWrapper } from 'enzyme'; import { InjectedRouter } from 'react-router'; +import { EditionKey } from '../apps/marketplace/utils'; export const mockEvent = { target: { blur() {} }, @@ -130,3 +131,54 @@ export function mockRouter(overrides: { push?: Function; replace?: Function } = ...overrides } as InjectedRouter; } + +export function mockAppState(overrides = {}): T.AppState { + return { + defaultOrganization: 'foo', + edition: EditionKey.community, + productionDatabase: true, + qualifiers: ['TRK'], + settings: {}, + version: '1.0', + ...overrides + }; +} + +export function mockComponent(overrides = {}): T.Component { + return { + breadcrumbs: [], + key: 'my-project', + name: 'MyProject', + organization: 'foo', + qualifier: 'TRK', + qualityGate: { isDefault: true, key: '30', name: 'Sonar way' }, + qualityProfiles: [mockQualityProfile()], + tags: [], + ...overrides + }; +} + +export function mockCurrentUser(overrides = {}): T.CurrentUser { + return { + isLoggedIn: true, + ...overrides + }; +} + +export function mockOrganization(overrides = {}): T.Organization { + return { + key: 'foo', + name: 'Foo', + ...overrides + }; +} + +export function mockQualityProfile(overrides = {}): T.ComponentQualityProfile { + return { + deleted: false, + key: 'my-qp', + language: 'ts', + name: 'Sonar way', + ...overrides + }; +} -- 2.39.5