Add tests for the scaffolder eventstream frontend client

Signed-off-by: Dominik Henneke <dominik.henneke@sda-se.com>
This commit is contained in:
Dominik Henneke
2021-10-25 14:17:37 +02:00
parent 9587f6f7d2
commit 95a48698bc
3 changed files with 74 additions and 6 deletions
+1
View File
@@ -77,6 +77,7 @@
"@types/jest": "^26.0.7",
"@types/node": "^14.14.32",
"cross-fetch": "^3.0.6",
"event-source-polyfill": "^1.0.25",
"msw": "^0.35.0"
},
"files": [
+70 -6
View File
@@ -14,12 +14,18 @@
* limitations under the License.
*/
import { ConfigReader } from '@backstage/core-app-api';
import { ScmIntegrations } from '@backstage/integration';
import { ScaffolderClient } from './api';
import { ConfigReader } from '@backstage/core-app-api';
const MockedEventSource = global.EventSource as jest.MockedClass<
typeof EventSource
>;
describe('api', () => {
const discoveryApi = {} as any;
const mockBaseUrl = 'http://backstage/api';
const discoveryApi = { getBaseUrl: async () => mockBaseUrl };
const identityApi = {} as any;
const scmIntegrationsApi = ScmIntegrations.fromConfig(
new ConfigReader({
@@ -32,10 +38,14 @@ describe('api', () => {
},
}),
);
const apiClient = new ScaffolderClient({
scmIntegrationsApi,
discoveryApi,
identityApi,
let apiClient: ScaffolderClient;
beforeEach(() => {
apiClient = new ScaffolderClient({
scmIntegrationsApi,
discoveryApi,
identityApi,
});
});
it('should return default and custom integrations', async () => {
@@ -51,4 +61,58 @@ describe('api', () => {
expect(allowedHosts).toContain(integration.host),
);
});
describe('streamEvents', () => {
describe('eventsource', () => {
it('should work', async () => {
MockedEventSource.prototype.addEventListener.mockImplementation(
(type, fn) => {
if (typeof fn !== 'function') {
return;
}
if (type === 'log') {
fn({
data: '{"id":1,"taskId":"a-random-id","type":"log","createdAt":"","body":{"message":"My log message"}}',
} as any);
} else if (type === 'completion') {
fn({
data: '{"id":2,"taskId":"a-random-id","type":"completion","createdAt":"","body":{"message":"Finished!"}}',
} as any);
}
},
);
const next = jest.fn();
await new Promise<void>(complete => {
apiClient
.streamLogs({ taskId: 'a-random-task-id' })
.subscribe({ next, complete });
});
expect(MockedEventSource).toBeCalledWith(
'http://backstage/api/v2/tasks/a-random-task-id/eventstream',
{ withCredentials: true },
);
expect(MockedEventSource.prototype.close).toBeCalled();
expect(next).toBeCalledTimes(2);
expect(next).toBeCalledWith({
id: 1,
taskId: 'a-random-id',
type: 'log',
createdAt: '',
body: { message: 'My log message' },
});
expect(next).toBeCalledWith({
id: 2,
taskId: 'a-random-id',
type: 'completion',
createdAt: '',
body: { message: 'Finished!' },
});
});
});
});
});
+3
View File
@@ -15,3 +15,6 @@
*/
import '@testing-library/jest-dom';
const { EventSourcePolyfill } = jest.requireMock('event-source-polyfill');
global.EventSource = EventSourcePolyfill;