|
| 1 | +import { beforeEach, describe, expect, it, jest } from '@jest/globals'; |
| 2 | +import { Command } from 'commander'; |
| 3 | +import { BuildStatus } from '../graphql/__generated__/graphql'; |
| 4 | +import { VisualApiRegion } from '../regions'; |
| 5 | + |
| 6 | +jest.unstable_mockModule('../api', () => ({ |
| 7 | + getApi: jest.fn().mockReturnValue({ |
| 8 | + createBuild: jest.fn(), |
| 9 | + finishBuild: jest.fn(), |
| 10 | + buildWithDiffsByCustomId: jest.fn(), |
| 11 | + buildWithDiffs: jest.fn(), |
| 12 | + }), |
| 13 | +})); |
| 14 | + |
| 15 | +const api = await import('../api'); |
| 16 | +const { command } = await import('./build'); |
| 17 | + |
| 18 | +const makeProgram = () => { |
| 19 | + return new Command().exitOverride().name('visual').addCommand(command()); |
| 20 | +}; |
| 21 | + |
| 22 | +// @ts-expect-error mock - prevent processes exiting during tests |
| 23 | +// eslint-disable-next-line @typescript-eslint/no-empty-function |
| 24 | +jest.spyOn(process, 'exit').mockImplementation(() => {}); |
| 25 | + |
| 26 | +const errorSpy = jest.spyOn(process.stderr, 'write'); |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + errorSpy.mockClear(); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('build create', () => { |
| 33 | + const createSpy = jest |
| 34 | + .spyOn(api.getApi({}), 'createBuild') |
| 35 | + .mockResolvedValue({ |
| 36 | + branch: null, |
| 37 | + defaultBranch: null, |
| 38 | + name: '', |
| 39 | + project: null, |
| 40 | + status: BuildStatus.Running, |
| 41 | + url: '', |
| 42 | + id: '1234', |
| 43 | + }); |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + (api.getApi as unknown as jest.Mock).mockClear(); |
| 47 | + createSpy.mockClear(); |
| 48 | + errorSpy.mockClear(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should parse a valid call with required "name"', () => { |
| 52 | + makeProgram().parse('npx visual build create --name test_build'.split(' ')); |
| 53 | + expect(createSpy).toBeCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should throw an error when name is omitted', () => { |
| 57 | + makeProgram().parse('npx visual build create'.split(' ')); |
| 58 | + expect(errorSpy).toBeCalledWith( |
| 59 | + expect.stringMatching(/required option .* not specified/), |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should parse all options', () => { |
| 64 | + makeProgram().parse( |
| 65 | + 'npx visual build create --name test_build --project project_name --branch branch_name --custom-id 12345'.split( |
| 66 | + ' ', |
| 67 | + ), |
| 68 | + ); |
| 69 | + expect(createSpy).toBeCalledWith({ |
| 70 | + name: 'test_build', |
| 71 | + project: 'project_name', |
| 72 | + branch: 'branch_name', |
| 73 | + customId: '12345', |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should pass the region to the getApi call', () => { |
| 78 | + makeProgram().parse( |
| 79 | + 'npx visual build create --name test_build --region eu-central-1'.split( |
| 80 | + ' ', |
| 81 | + ), |
| 82 | + ); |
| 83 | + expect(api.getApi).toBeCalledWith( |
| 84 | + expect.objectContaining({ |
| 85 | + region: VisualApiRegion.fromName('eu-central-1'), |
| 86 | + }), |
| 87 | + ); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe('build finish', () => { |
| 92 | + const finishSpy = jest |
| 93 | + .spyOn(api.getApi({}), 'finishBuild') |
| 94 | + .mockResolvedValue({ |
| 95 | + name: '', |
| 96 | + status: BuildStatus.Equal, |
| 97 | + url: '', |
| 98 | + id: '1234', |
| 99 | + }); |
| 100 | + |
| 101 | + beforeEach(() => { |
| 102 | + (api.getApi as unknown as jest.Mock).mockClear(); |
| 103 | + finishSpy.mockClear(); |
| 104 | + errorSpy.mockClear(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should parse a valid call with required "build-id"', () => { |
| 108 | + makeProgram().parse('npx visual build finish --build-id 1234'.split(' ')); |
| 109 | + expect(finishSpy).toBeCalledWith({ uuid: '1234' }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should throw if neither a build id or custom id not provided', () => { |
| 113 | + makeProgram().parse('npx visual build finish'.split(' ')); |
| 114 | + expect(errorSpy).toBeCalledWith( |
| 115 | + expect.stringMatching(/--build-id or --custom-id needs to be specified/), |
| 116 | + ); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('build status', () => { |
| 121 | + const statusSpy = jest |
| 122 | + .spyOn(api.getApi({}), 'buildWithDiffs') |
| 123 | + .mockResolvedValue({ |
| 124 | + __typename: 'Build', |
| 125 | + id: '1234', |
| 126 | + name: '', |
| 127 | + status: BuildStatus.Equal, |
| 128 | + project: '', |
| 129 | + url: '', |
| 130 | + branch: null, |
| 131 | + diffs: { |
| 132 | + nodes: [], |
| 133 | + }, |
| 134 | + }); |
| 135 | + |
| 136 | + beforeEach(() => { |
| 137 | + (api.getApi as unknown as jest.Mock).mockClear(); |
| 138 | + statusSpy.mockClear(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should parse a valid call with required "build-id"', () => { |
| 142 | + makeProgram().parse('npx visual build status --build-id 1234'.split(' ')); |
| 143 | + expect(statusSpy).toBeCalledWith('1234'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should throw if neither a build id or custom id not provided', () => { |
| 147 | + makeProgram().parse('npx visual build status'.split(' ')); |
| 148 | + expect(errorSpy).toBeCalledWith( |
| 149 | + expect.stringMatching(/--build-id or --custom-id needs to be specified/), |
| 150 | + ); |
| 151 | + }); |
| 152 | +}); |
0 commit comments