Skip to content

Commit

Permalink
Paginate list clusters (#307)
Browse files Browse the repository at this point in the history
* Paginate list clusters, add unit tests for ListClusters

Signed-off-by: Judy Ng <[email protected]>
  • Loading branch information
judysng authored Feb 19, 2024
1 parent 85edc22 commit af3394f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
109 changes: 109 additions & 0 deletions frontend/src/__tests__/ListClusters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {ListClusters} from '../model'
import {ClusterInfoSummary, ClusterStatus} from '../types/clusters'
import {CloudFormationStackStatus} from '../types/base'

const mockCluster1: ClusterInfoSummary = {
clusterName: 'test-cluster-1',
clusterStatus: ClusterStatus.CreateComplete,
version: '3.8.0',
cloudformationStackArn: 'arn',
region: 'region',
cloudformationStackStatus: CloudFormationStackStatus.CreateComplete,
}

const mockCluster2: ClusterInfoSummary = {
clusterName: 'test-cluster-2',
clusterStatus: ClusterStatus.CreateComplete,
version: '3.8.0',
cloudformationStackArn: 'arn',
region: 'region',
cloudformationStackStatus: CloudFormationStackStatus.CreateComplete,
}

const mockGet = jest.fn()

jest.mock('axios', () => ({
create: () => ({
get: (...args: unknown[]) => mockGet(...args),
}),
}))

describe('given a ListClusters command', () => {
describe('when the clusters use pagination to be listed', () => {
beforeEach(() => {
const mockResponse1 = {
nextToken: 'asdfghjkl',
clusters: [mockCluster1],
}

const mockResponse2 = {
clusters: [mockCluster2],
}
mockGet
.mockResolvedValueOnce({data: mockResponse1})
.mockResolvedValueOnce({data: mockResponse2})
})

it('should return the list of all clusters', async () => {
const data = await ListClusters()
expect(data).toEqual([mockCluster1, mockCluster2])
})
})

describe('when the clusters are listed in one API call', () => {
beforeEach(() => {
const mockResponse1 = {
clusters: [mockCluster1, mockCluster2],
}
mockGet.mockResolvedValueOnce({data: mockResponse1})
})

it('should return the list of all clusters', async () => {
const data = await ListClusters()
expect(data).toEqual([mockCluster1, mockCluster2])
})
})

describe('when there are no clusters', () => {
beforeEach(() => {
const mockResponse1 = {
clusters: [],
}
mockGet.mockResolvedValueOnce({data: mockResponse1})
})

it('should return empty list', async () => {
const data = await ListClusters()
expect(data).toEqual([])
})
})

describe('when the list cluster fails', () => {
let mockError: any

beforeEach(() => {
mockError = {
response: {
data: {
message: 'some-error-messasge',
},
},
}
mockGet.mockRejectedValueOnce(mockError)
})

it('should throw the error', async () => {
try {
await ListClusters()
} catch (e) {
expect(e).toEqual({
response: {
data: {
message: 'some-error-messasge',
},
},
})
}
})
})
})
12 changes: 9 additions & 3 deletions frontend/src/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,15 @@ function DeleteCluster(clusterName: any, callback?: Callback) {
async function ListClusters(): Promise<ClusterInfoSummary[]> {
var url = 'api?path=/v3/clusters'
try {
const {data} = await request('get', url)
setState(['clusters', 'list'], data?.clusters)
return data?.clusters || []
var response = await request('get', url)
var clusters = response.data.clusters
while ('nextToken' in response.data) {
const urlToken = `${url}&nextToken=${response.data.nextToken}`
response = await request('get', urlToken)
clusters = clusters.concat(response.data.clusters)
}
setState(['clusters', 'list'], clusters)
return clusters || []
} catch (error) {
if ((error as any).response) {
notify(`Error: ${(error as any).response.data.message}`, 'error')
Expand Down

2 comments on commit af3394f

@mission-coliveros
Copy link

@mission-coliveros mission-coliveros commented on af3394f Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can anyone provide a timeline on when a release will be created including this change? This issue is actively making the UI unusable for us, and we'd appreciate it if we could see a release as soon as possible

@enrico-usai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been released as part of 2024.03.0

Please sign in to comment.