grafana/public/app/features/folders/state/actions.test.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

69 lines
2.3 KiB
TypeScript

import { Observable, of, throwError } from 'rxjs';
import { thunkTester } from 'test/core/thunk/thunkTester';
import { FetchResponse } from '@grafana/runtime';
import { notifyApp } from 'app/core/actions';
import { createWarningNotification } from 'app/core/copy/appNotification';
import { backendSrv } from 'app/core/services/backend_srv';
import { checkFolderPermissions } from './actions';
import { setCanViewFolderPermissions } from './reducers';
describe('folder actions', () => {
let fetchSpy: jest.SpyInstance<Observable<FetchResponse<unknown>>>;
beforeAll(() => {
fetchSpy = jest.spyOn(backendSrv, 'fetch');
});
afterAll(() => {
fetchSpy.mockRestore();
});
function mockFetch(resp: Observable<any>) {
fetchSpy.mockReturnValueOnce(resp);
}
const folderUid = 'abc123';
describe('checkFolderPermissions', () => {
it('should dispatch true when the api call is successful', async () => {
mockFetch(of({}));
const dispatchedActions = await thunkTester({})
.givenThunk(checkFolderPermissions)
.whenThunkIsDispatched(folderUid);
expect(dispatchedActions).toEqual([setCanViewFolderPermissions(true)]);
});
it('should dispatch just "false" when the api call fails with 403', async () => {
mockFetch(throwError(() => ({ status: 403, data: { message: 'Access denied' } })));
const dispatchedActions = await thunkTester({})
.givenThunk(checkFolderPermissions)
.whenThunkIsDispatched(folderUid);
expect(dispatchedActions).toEqual([setCanViewFolderPermissions(false)]);
});
it('should also dispatch a notification when the api call fails with an error other than 403', async () => {
mockFetch(throwError(() => ({ status: 500, data: { message: 'Server error' } })));
const dispatchedActions = await thunkTester({})
.givenThunk(checkFolderPermissions)
.whenThunkIsDispatched(folderUid);
const notificationAction = notifyApp(
createWarningNotification('Error checking folder permissions', 'Server error')
);
notificationAction.payload.id = expect.any(String);
notificationAction.payload.timestamp = expect.any(Number);
expect(dispatchedActions).toEqual([
expect.objectContaining(notificationAction),
setCanViewFolderPermissions(false),
]);
});
});
});