Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Fix "Disable Rollback button not working" #343

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions frontend/src/__tests__/CreateCluster.test.ts
Original file line number Diff line number Diff line change
@@ -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&region=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&region=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&region=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',
})
})
})
})
4 changes: 2 additions & 2 deletions frontend/src/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? `&region=${region}` : ''
var body = {clusterName: clusterName, clusterConfiguration: clusterConfig}
request('post', url, body)
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/old-pages/Configure/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -82,6 +83,7 @@ function handleCreate(handleClose: any, navigate: any) {
clusterName,
clusterConfig,
region,
selectedRegion,
disableRollback,
dryRun,
successHandler,
Expand All @@ -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) => {
Expand All @@ -122,6 +125,7 @@ function handleDryRun() {
clusterName,
clusterConfig,
region,
selectedRegion,
disableRollback,
dryRun,
successHandler,
Expand Down