From: Wouter Admiraal Date: Mon, 25 Mar 2019 16:12:36 +0000 (+0100) Subject: Use new mocks in branch helper tests X-Git-Tag: 7.8~490 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=687ced1be70bf111c4c5617ca8d224af3f511441;p=sonarqube.git Use new mocks in branch helper tests --- diff --git a/server/sonar-web/src/main/js/helpers/__tests__/branches-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/branches-test.ts index ef494587ddf..d84f430be0c 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/branches-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/branches-test.ts @@ -18,31 +18,37 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { sortBranchesAsTree, isSameBranchLike } from '../branches'; +import { + mockShortLivingBranch, + mockLongLivingBranch, + mockPullRequest, + mockMainBranch +} from '../testMocks'; describe('#sortBranchesAsTree', () => { it('sorts main branch and short-living branches', () => { - const main = mainBranch(); - const foo = shortLivingBranch({ name: 'foo' }); - const bar = shortLivingBranch({ name: 'bar' }); + const main = mockMainBranch(); + const foo = mockShortLivingBranch({ name: 'foo' }); + const bar = mockShortLivingBranch({ name: 'bar' }); expect(sortBranchesAsTree([main, foo, bar])).toEqual([main, bar, foo]); }); it('sorts main branch and long-living branches', () => { - const main = mainBranch(); - const foo = longLivingBranch({ name: 'foo' }); - const bar = longLivingBranch({ name: 'bar' }); + const main = mockMainBranch(); + const foo = mockLongLivingBranch({ name: 'foo' }); + const bar = mockLongLivingBranch({ name: 'bar' }); expect(sortBranchesAsTree([main, foo, bar])).toEqual([main, bar, foo]); }); it('sorts all types of branches', () => { - const main = mainBranch(); - const shortFoo = shortLivingBranch({ name: 'shortFoo', mergeBranch: 'master' }); - const shortBar = shortLivingBranch({ name: 'shortBar', mergeBranch: 'longBaz' }); - const shortPre = shortLivingBranch({ name: 'shortPre', mergeBranch: 'shortFoo' }); - const longBaz = longLivingBranch({ name: 'longBaz' }); - const longQux = longLivingBranch({ name: 'longQux' }); - const longQwe = longLivingBranch({ name: 'longQwe' }); - const pr = pullRequest({ base: 'master' }); + const main = mockMainBranch(); + const shortFoo = mockShortLivingBranch({ name: 'shortFoo', mergeBranch: 'master' }); + const shortBar = mockShortLivingBranch({ name: 'shortBar', mergeBranch: 'longBaz' }); + const shortPre = mockShortLivingBranch({ name: 'shortPre', mergeBranch: 'shortFoo' }); + const longBaz = mockLongLivingBranch({ name: 'longBaz' }); + const longQux = mockLongLivingBranch({ name: 'longQux' }); + const longQwe = mockLongLivingBranch({ name: 'longQwe' }); + const pr = mockPullRequest({ base: 'master' }); // - main - main // - shortFoo - shortFoo // - shortPre - shortPre @@ -58,10 +64,10 @@ describe('#sortBranchesAsTree', () => { describe('#isSameBranchLike', () => { it('compares different kinds', () => { - const main = mainBranch(); - const short = shortLivingBranch({ name: 'foo' }); - const long = longLivingBranch({ name: 'foo' }); - const pr = pullRequest(); + const main = mockMainBranch(); + const short = mockShortLivingBranch({ name: 'foo' }); + const long = mockLongLivingBranch({ name: 'foo' }); + const pr = mockPullRequest(); expect(isSameBranchLike(main, pr)).toBeFalsy(); expect(isSameBranchLike(main, short)).toBeFalsy(); expect(isSameBranchLike(main, long)).toBeFalsy(); @@ -72,58 +78,31 @@ describe('#isSameBranchLike', () => { it('compares pull requests', () => { expect( - isSameBranchLike(pullRequest({ key: '1234' }), pullRequest({ key: '1234' })) + isSameBranchLike(mockPullRequest({ key: '1234' }), mockPullRequest({ key: '1234' })) ).toBeTruthy(); expect( - isSameBranchLike(pullRequest({ key: '1234' }), pullRequest({ key: '5678' })) + isSameBranchLike(mockPullRequest({ key: '1234' }), mockPullRequest({ key: '5678' })) ).toBeFalsy(); }); it('compares branches', () => { expect( - isSameBranchLike(longLivingBranch({ name: 'foo' }), longLivingBranch({ name: 'foo' })) + isSameBranchLike(mockLongLivingBranch({ name: 'foo' }), mockLongLivingBranch({ name: 'foo' })) ).toBeTruthy(); expect( - isSameBranchLike(shortLivingBranch({ name: 'foo' }), shortLivingBranch({ name: 'foo' })) + isSameBranchLike( + mockShortLivingBranch({ name: 'foo' }), + mockShortLivingBranch({ name: 'foo' }) + ) ).toBeTruthy(); expect( - isSameBranchLike(longLivingBranch({ name: 'foo' }), longLivingBranch({ name: 'bar' })) + isSameBranchLike(mockLongLivingBranch({ name: 'foo' }), mockLongLivingBranch({ name: 'bar' })) ).toBeFalsy(); expect( - isSameBranchLike(shortLivingBranch({ name: 'foo' }), shortLivingBranch({ name: 'bar' })) + isSameBranchLike( + mockShortLivingBranch({ name: 'foo' }), + mockShortLivingBranch({ name: 'bar' }) + ) ).toBeFalsy(); }); }); - -function mainBranch(): T.MainBranch { - return { isMain: true, name: 'master' }; -} - -function shortLivingBranch(overrides?: Partial): T.ShortLivingBranch { - const status = { bugs: 0, codeSmells: 0, qualityGateStatus: 'OK', vulnerabilities: 0 }; - return { - isMain: false, - mergeBranch: 'master', - name: 'foo', - status, - type: 'SHORT', - ...overrides - }; -} - -function longLivingBranch(overrides?: Partial): T.LongLivingBranch { - const status = { qualityGateStatus: 'OK' }; - return { isMain: false, name: 'foo', status, type: 'LONG', ...overrides }; -} - -function pullRequest(overrides?: Partial): T.PullRequest { - const status = { bugs: 0, codeSmells: 0, qualityGateStatus: 'OK', vulnerabilities: 0 }; - return { - base: 'master', - branch: 'feature', - key: '1234', - status, - title: 'Random Name', - ...overrides - }; -} diff --git a/server/sonar-web/src/main/js/helpers/testMocks.ts b/server/sonar-web/src/main/js/helpers/testMocks.ts index a14df2435cc..5ba89478c2d 100644 --- a/server/sonar-web/src/main/js/helpers/testMocks.ts +++ b/server/sonar-web/src/main/js/helpers/testMocks.ts @@ -387,3 +387,12 @@ export function mockDocumentationEntry( ...overrides }; } + +export function mockMainBranch(overrides: Partial = {}): T.MainBranch { + return { + analysisDate: '2018-01-01', + isMain: true, + name: 'master', + ...overrides + }; +}