-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test(backend): add missing unit tests Signed-off-by: Oleksii Kurinnyi <[email protected]> * test(frontend): add missing unit tests Signed-off-by: Oleksii Kurinnyi <[email protected]> * test: remove codecov flag --------- Signed-off-by: Oleksii Kurinnyi <[email protected]>
- Loading branch information
Showing
37 changed files
with
905 additions
and
12 deletions.
There are no files selected for viewing
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
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
20 changes: 20 additions & 0 deletions
20
packages/dashboard-backend/src/models/__tests__/index.spec.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,20 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as models from '@/models'; | ||
|
||
describe('models', () => { | ||
it('should export models', () => { | ||
// this makes the coverage tool happy | ||
expect(models).toBeDefined(); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/dashboard-backend/src/models/__tests__/restParams.spec.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,20 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as restParams from '@/models/restParams'; | ||
|
||
describe('restParams', () => { | ||
it('should export restParams', () => { | ||
// this makes the coverage tool happy | ||
expect(restParams).toBeDefined(); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
packages/dashboard-backend/src/services/__tests__/ObjectsWatcher.spec.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,80 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { api } from '@eclipse-che/common'; | ||
|
||
import { IWatcherService } from '@/devworkspaceClient'; | ||
import { ObjectsWatcher } from '@/services/ObjectsWatcher'; | ||
|
||
describe('ObjectsWatcher', () => { | ||
const message = 'message'; | ||
const channel = api.webSocket.Channel.DEV_WORKSPACE; | ||
|
||
const mockListener = jest.fn(); | ||
const mockParams = { param: 'value' }; | ||
|
||
const mockStopWatching = jest.fn(); | ||
const mockWatchInNamespace = jest | ||
.fn() | ||
.mockImplementation(listener => Promise.resolve(listener(message))); | ||
|
||
let objectsWatcher: ObjectsWatcher<unknown>; | ||
|
||
beforeEach(() => { | ||
const apiService = { | ||
stopWatching: () => mockStopWatching(), | ||
watchInNamespace: (...args) => mockWatchInNamespace(...args), | ||
} as IWatcherService; | ||
objectsWatcher = new ObjectsWatcher(apiService, channel); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('start watching w/o observer', async () => { | ||
await objectsWatcher.start('test', mockParams); | ||
|
||
expect(mockWatchInNamespace).toHaveBeenCalledWith(expect.any(Function), mockParams); | ||
}); | ||
|
||
test('watch changes with observer', async () => { | ||
const observer = { | ||
update: mockListener, | ||
}; | ||
objectsWatcher.attach(observer); | ||
|
||
await objectsWatcher.start('test', mockParams); | ||
|
||
expect(mockListener).toHaveBeenCalledWith(channel, 'message'); | ||
expect(mockWatchInNamespace).toHaveBeenCalledWith(expect.any(Function), mockParams); | ||
}); | ||
|
||
test('detach observer', async () => { | ||
const observer = { | ||
update: mockListener, | ||
}; | ||
objectsWatcher.attach(observer); | ||
objectsWatcher.detach(); | ||
|
||
await objectsWatcher.start('test', mockParams); | ||
|
||
expect(mockListener).not.toHaveBeenCalled(); | ||
expect(mockWatchInNamespace).toHaveBeenCalledWith(expect.any(Function), mockParams); | ||
}); | ||
|
||
test('stop watching', async () => { | ||
objectsWatcher.stop(); | ||
|
||
expect(mockStopWatching).toHaveBeenCalled(); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
packages/dashboard-backend/src/services/__tests__/SubscriptionManager.spec.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,58 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { api } from '@eclipse-che/common'; | ||
import MockWebSocket from 'jest-websocket-mock'; | ||
import WS from 'ws'; | ||
|
||
import { SubscriptionManager } from '@/services/SubscriptionManager'; | ||
import { NotificationMessage } from '@/services/types/Observer'; | ||
|
||
describe('SubscriptionManager', () => { | ||
const channel = api.webSocket.Channel.DEV_WORKSPACE; | ||
|
||
const ws = new MockWebSocket('ws://localhost') as unknown as WS; | ||
const spyWsSend = jest.spyOn(ws, 'send'); | ||
|
||
let subscriptionManager: SubscriptionManager; | ||
|
||
beforeEach(() => { | ||
subscriptionManager = new SubscriptionManager(ws); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('subscribe and update', () => { | ||
subscriptionManager.subscribe(channel); | ||
|
||
const message = { | ||
eventPhase: api.webSocket.EventPhase.ADDED, | ||
} as NotificationMessage; | ||
subscriptionManager.update(channel, message); | ||
|
||
expect(spyWsSend).toHaveBeenCalled(); | ||
}); | ||
|
||
test('unsubscribe all channels', () => { | ||
subscriptionManager.subscribe(channel); | ||
subscriptionManager.unsubscribeAll(); | ||
|
||
const message = { | ||
eventPhase: api.webSocket.EventPhase.ADDED, | ||
} as NotificationMessage; | ||
subscriptionManager.update(channel, message); | ||
|
||
expect(spyWsSend).not.toHaveBeenCalled(); | ||
}); | ||
}); |
88 changes: 88 additions & 0 deletions
88
packages/dashboard-backend/src/services/__tests__/helpers.spec.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,88 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { authenticationHeaderSchema } from '@/constants/schemas'; | ||
import * as helpers from '@/services/helpers'; | ||
|
||
describe('helpers', () => { | ||
test('delay', async () => { | ||
jest.useFakeTimers(); | ||
|
||
const mockCallback = jest.fn(); | ||
const promise = helpers.delay(1000).then(mockCallback); | ||
|
||
await jest.advanceTimersByTimeAsync(500); | ||
expect(mockCallback).not.toHaveBeenCalled(); | ||
|
||
await jest.advanceTimersByTimeAsync(500); | ||
expect(promise).resolves.toBeUndefined(); | ||
expect(mockCallback).toHaveBeenCalled(); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
|
||
test('getSchema', () => { | ||
const schema = helpers.getSchema({ | ||
tags: ['test'], | ||
namespacedSchema: { | ||
type: 'object', | ||
properties: { | ||
namespace: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
body: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(schema).toEqual({ | ||
schema: { | ||
headers: authenticationHeaderSchema, | ||
namespacedSchema: { | ||
properties: { | ||
namespace: { | ||
type: 'string', | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
security: [ | ||
{ | ||
Authorization: '', | ||
}, | ||
], | ||
tags: ['test'], | ||
body: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('createFastifyError', () => { | ||
const error = helpers.createFastifyError('test', '500'); | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.statusCode).toEqual(500); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
packages/dashboard-backend/src/services/kubeclient/helpers/__tests__/index.spec.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,46 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as k8s from '@kubernetes/client-node'; | ||
|
||
import { isOpenShift } from '@/services/kubeclient/helpers'; | ||
|
||
const mockFindApi = jest.fn(); | ||
jest.mock('@/helpers/findApi', () => ({ | ||
findApi: jest.fn().mockImplementation(() => mockFindApi()), | ||
})); | ||
|
||
describe('isOpenShift', () => { | ||
it('should return true if project.openshift.io API is available', async () => { | ||
mockFindApi.mockResolvedValue(true); | ||
|
||
const res = await isOpenShift({} as k8s.ApisApi); | ||
|
||
expect(res).toBe(true); | ||
}); | ||
|
||
it('should return false if project.openshift.io API is not available', async () => { | ||
mockFindApi.mockResolvedValue(false); | ||
|
||
const res = await isOpenShift({} as k8s.ApisApi); | ||
|
||
expect(res).toBe(false); | ||
}); | ||
|
||
it('should throw an error if findApi throws an error', async () => { | ||
mockFindApi.mockRejectedValue(new Error('find-api-error')); | ||
|
||
await expect(isOpenShift({} as k8s.ApisApi)).rejects.toThrow( | ||
`Can't evaluate target platform: find-api-error`, | ||
); | ||
}); | ||
}); |
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
File renamed without changes.
20 changes: 20 additions & 0 deletions
20
packages/dashboard-backend/src/services/types/__tests__/Observer.spec.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,20 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as Observer from '@/services/types/Observer'; | ||
|
||
describe('Observer', () => { | ||
it('should export Subject', () => { | ||
// this makes the coverage tool happy | ||
expect(Observer).toBeDefined(); | ||
}); | ||
}); |
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
29 changes: 29 additions & 0 deletions
29
packages/dashboard-frontend/src/Layout/ErrorBoundary/__mocks__/index.tsx
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,29 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { Props } from '@/Layout/ErrorBoundary'; | ||
|
||
export const errorMessage = 'Error Boundary Message'; | ||
|
||
export class ErrorBoundary extends React.PureComponent<Props> { | ||
public render(): React.ReactElement { | ||
return ( | ||
<div> | ||
Mock ErrorBoundary component | ||
<button onClick={() => this.props.onError(errorMessage)}>onError</button> | ||
{this.props.children} | ||
</div> | ||
); | ||
} | ||
} |
Oops, something went wrong.