grafana/public/test/jest-setup.ts

71 lines
1.9 KiB
TypeScript
Raw Normal View History

import { configure } from 'enzyme';
import { EventBusSrv } from '@grafana/data';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
2018-06-12 09:14:22 -05:00
import $ from 'jquery';
import 'mutationobserver-shim';
const testAppEvents = new EventBusSrv();
const global = window as any;
global.$ = global.jQuery = $;
2021-06-17 13:20:27 -05:00
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(global, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
import '../vendor/flot/jquery.flot';
import '../vendor/flot/jquery.flot.time';
2018-06-12 09:14:22 -05:00
import angular from 'angular';
angular.module('grafana', ['ngRoute']);
angular.module('grafana.services', ['ngRoute', '$strap.directives']);
angular.module('grafana.panels', []);
angular.module('grafana.controllers', []);
angular.module('grafana.directives', []);
angular.module('grafana.filters', []);
angular.module('grafana.routes', ['ngRoute']);
jest.mock('../app/core/core', () => ({ appEvents: testAppEvents }));
jest.mock('../app/angular/partials', () => ({}));
jest.mock('../app/features/plugins/plugin_loader', () => ({}));
configure({ adapter: new Adapter() });
2018-06-12 09:14:22 -05:00
2018-09-07 10:55:38 -05:00
const localStorageMock = (() => {
let store: any = {};
return {
getItem: (key: string) => {
return store[key];
},
setItem: (key: string, value: any) => {
store[key] = value.toString();
},
2018-09-07 10:55:38 -05:00
clear: () => {
store = {};
},
removeItem: (key: string) => {
delete store[key];
},
};
})();
global.localStorage = localStorageMock;
const throwUnhandledRejections = () => {
process.on('unhandledRejection', (err) => {
throw err;
});
};
throwUnhandledRejections();