diff --git a/frontend/src/__tests__/CreateCluster.test.ts b/frontend/src/__tests__/CreateCluster.test.ts new file mode 100644 index 00000000..8e25fc6b --- /dev/null +++ b/frontend/src/__tests__/CreateCluster.test.ts @@ -0,0 +1,139 @@ +import {CreateCluster} from '../model' + +const mockPost = jest.fn() + +jest.mock('axios', () => ({ + create: () => ({ + post: (...args: unknown[]) => mockPost(...args), + }), +})) + +describe('given a CreateCluster command and a cluster configuration', () => { + const clusterName = 'any-name' + const clusterConfiguration = {} + const mockRegion = 'some-region' + const mockSelectedRegion = 'some-region' + + describe('when the cluster can be created successfully', () => { + beforeEach(() => { + jest.clearAllMocks() + const mockResponse = { + status: 202, + data: { + some: 'data', + }, + } + mockPost.mockResolvedValueOnce(mockResponse) + }) + + it('should emit the API request', async () => { + const expectedBody = { + clusterConfiguration, + clusterName, + } + + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + ) + expect(mockPost).toHaveBeenCalledTimes(1) + expect(mockPost).toHaveBeenCalledWith( + 'api?path=/v3/clusters®ion=some-region', + expectedBody, + expect.any(Object), + ) + }) + + it('should call the success callback', async () => { + const mockSuccessCallback = jest.fn() + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + false, + false, + mockSuccessCallback, + ) + expect(mockSuccessCallback).toHaveBeenCalledTimes(1) + expect(mockSuccessCallback).toHaveBeenCalledWith({some: 'data'}) + }) + + describe('when a dryrun is requested', () => { + const mockDryRun = true + + it('should add set the dryrun query parameter', async () => { + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + false, + mockDryRun, + ) + expect(mockPost).toHaveBeenCalledTimes(1) + expect(mockPost).toHaveBeenCalledWith( + 'api?path=/v3/clusters&dryrun=true®ion=some-region', + expect.any(Object), + expect.any(Object), + ) + }) + }) + + describe('when stack rollback on failure is disabled', () => { + const mockDisableRollback = true + + it('should add set the rollbackOnFailure query parameter', async () => { + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + mockDisableRollback, + ) + expect(mockPost).toHaveBeenCalledTimes(1) + expect(mockPost).toHaveBeenCalledWith( + 'api?path=/v3/clusters&rollbackOnFailure=false®ion=some-region', + expect.any(Object), + expect.any(Object), + ) + }) + }) + }) + + describe('when the cluster creation fails', () => { + let mockError: any + + beforeEach(() => { + mockError = { + response: { + data: { + message: 'some-error-message', + }, + }, + } + mockPost.mockRejectedValueOnce(mockError) + }) + + it('should call the error callback', async () => { + const mockErrorCallback = jest.fn() + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + false, + false, + undefined, + mockErrorCallback, + ) + await Promise.resolve() + expect(mockErrorCallback).toHaveBeenCalledTimes(1) + expect(mockErrorCallback).toHaveBeenCalledWith({ + message: 'some-error-message', + }) + }) + }) +}) diff --git a/frontend/src/model.tsx b/frontend/src/model.tsx index edc20f76..3d42e389 100644 --- a/frontend/src/model.tsx +++ b/frontend/src/model.tsx @@ -97,15 +97,15 @@ function CreateCluster( clusterName: any, clusterConfig: any, region: string, + selectedRegion: string, disableRollback = false, dryrun = false, successCallback?: Callback, errorCallback?: Callback, ) { - const selectedRegion = getState(['app', 'selectedRegion']) var url = 'api?path=/v3/clusters' url += dryrun ? '&dryrun=true' : '' - url += disableRollback ? '&disableRollback=true' : '' + url += disableRollback ? '&rollbackOnFailure=false' : '' url += region ? `®ion=${region}` : '' var body = {clusterName: clusterName, clusterConfiguration: clusterConfig} request('post', url, body) diff --git a/frontend/src/old-pages/Configure/Create.tsx b/frontend/src/old-pages/Configure/Create.tsx index e874aef8..9fab1eb6 100644 --- a/frontend/src/old-pages/Configure/Create.tsx +++ b/frontend/src/old-pages/Configure/Create.tsx @@ -49,6 +49,7 @@ function handleCreate(handleClose: any, navigate: any) { const clusterConfig = getState(configPath) || '' const dryRun = false const region = getState(['app', 'wizard', 'config', 'Region']) + const selectedRegion = getState(['app', 'selectedRegion']) var errHandler = (err: any) => { setState(['app', 'wizard', 'errors', 'create'], err) setState(['app', 'wizard', 'pending'], false) @@ -82,6 +83,7 @@ function handleCreate(handleClose: any, navigate: any) { clusterName, clusterConfig, region, + selectedRegion, disableRollback, dryRun, successHandler, @@ -96,6 +98,7 @@ function handleDryRun() { const forceUpdate = getState(['app', 'wizard', 'forceUpdate']) const clusterConfig = getState(configPath) || '' const region = getState(['app', 'wizard', 'config', 'Region']) + const selectedRegion = getState(['app', 'selectedRegion']) const disableRollback = false const dryRun = true var errHandler = (err: any) => { @@ -122,6 +125,7 @@ function handleDryRun() { clusterName, clusterConfig, region, + selectedRegion, disableRollback, dryRun, successHandler,