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

Commit

Permalink
Merge pull request #343 from aws-samples/fix-disable-rollback
Browse files Browse the repository at this point in the history
Fix "Disable Rollback button not working"
  • Loading branch information
mendaomn authored Oct 31, 2022
2 parents dfc81c2 + 5db9a57 commit d01d5f4
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
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

0 comments on commit d01d5f4

Please sign in to comment.