forked from backstage/community-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds tests for OptimizationsApiClientProxy.ts
Signed-off-by: Jonathan Kilzi <jkilzi@redhat.com>
- Loading branch information
Showing
4 changed files
with
4,398 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
...ugins/resource-optimization-common/src/client/private/OptimizationsApiClientProxy.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** @jest-environment node */ | ||
import { http, HttpResponse } from 'msw'; | ||
import { setupServer } from 'msw/node'; | ||
import { OptimizationsApiClientProxy } from '../OptimizationsApiClientProxy'; | ||
import { DiscoveryApi } from '../../generated/types/discovery'; | ||
import { GetRecommendationByIdRequest } from '../../models/requests'; | ||
import { makePlotsDataPropertyPathWithTerm } from './test-helpers'; | ||
import * as GetRecommendationByIdMockResponse from './fixtures/GetRecommendationByIdMockResponse.json'; | ||
|
||
const MOCK_BASE_URL = 'http://backstage:1234/api/proxy'; | ||
const mockDiscoveryApi: DiscoveryApi = { | ||
async getBaseUrl(_pluginId: string): Promise<string> { | ||
return MOCK_BASE_URL; | ||
}, | ||
}; | ||
const server = setupServer( | ||
http.get(`${MOCK_BASE_URL}/token`, _info => | ||
HttpResponse.json({ | ||
accessToken: 'hereisyourtokensir', | ||
expiresAt: 1234567890, | ||
}), | ||
), | ||
); | ||
|
||
describe('OptimizationsApiClientProxy.ts', () => { | ||
let client: OptimizationsApiClientProxy; | ||
|
||
beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); | ||
beforeEach(() => { | ||
client = new OptimizationsApiClientProxy({ | ||
discoveryApi: mockDiscoveryApi, | ||
}); | ||
}); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
describe('getRecommendationById', () => { | ||
describe('responses', () => { | ||
it("should not transform the plotsData's date-string properties", async () => { | ||
expect.assertions(3); | ||
|
||
// Arrange | ||
server.use( | ||
http.get( | ||
`${MOCK_BASE_URL}/cost-management/v1/recommendations/openshift/:id`, | ||
_info => HttpResponse.json(GetRecommendationByIdMockResponse), | ||
), | ||
); | ||
|
||
// Act | ||
const request: GetRecommendationByIdRequest = { | ||
path: { recommendationId: 'abcdef01-abcd-abcd-abcd-0123456789abc' }, | ||
query: {}, | ||
}; | ||
const response = await client.getRecommendationById(request); | ||
const json = await response.json(); | ||
|
||
// Assert | ||
expect(json).toHaveProperty( | ||
makePlotsDataPropertyPathWithTerm( | ||
'short', | ||
'2024-07-30T17:00:05.000Z', | ||
), | ||
); | ||
expect(json).toHaveProperty( | ||
makePlotsDataPropertyPathWithTerm( | ||
'medium', | ||
'2024-07-25T11:00:05.000Z', | ||
), | ||
); | ||
expect(json).toHaveProperty( | ||
makePlotsDataPropertyPathWithTerm('long', '2024-07-17T11:00:05.000Z'), | ||
); | ||
}); | ||
|
||
it('should not transform to snake_case the recommendationId path parameter', async () => { | ||
expect.assertions(2); | ||
|
||
// Arrange | ||
const spyOnDefaultClientGetRecommendationById = jest.spyOn( | ||
// @ts-ignore (because defaultClient is private 🤫) | ||
client.defaultClient, | ||
'getRecommendationById', | ||
); | ||
server.use( | ||
http.get( | ||
`${MOCK_BASE_URL}/cost-management/v1/recommendations/openshift/:id`, | ||
_info => HttpResponse.json(GetRecommendationByIdMockResponse), | ||
), | ||
); | ||
|
||
// Act | ||
const request: GetRecommendationByIdRequest = { | ||
path: { recommendationId: 'abcdef01-abcd-abcd-abcd-0123456789abc' }, | ||
query: { cpuUnit: 'cores' }, | ||
}; | ||
await client.getRecommendationById(request); | ||
|
||
// Assert | ||
expect( | ||
spyOnDefaultClientGetRecommendationById.mock.lastCall?.[0], | ||
).toHaveProperty('path.recommendationId'); | ||
expect( | ||
spyOnDefaultClientGetRecommendationById.mock.lastCall?.[0], | ||
).toHaveProperty('query.cpu_unit'); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.