Files
superset2/superset/assets/spec/javascripts/sqllab/reducers_spec.js
Christine Chambers 9029701f24 [refactor] Migrate from Mocha+Chai to Jest (#6079)
* [refactor] Migrate from Mocha+Chai to Jest

This change migrates all the existing unit tests
- to Jest's global expect and matchers from chai's imported expect, asserts and matchers.
- to Jest's describe/test from mocha's describe/it

The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks:
1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank.
2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in it with Jest. :)

This change also removes dependencies to Mocha and Chai.

* Remove the test:one command as it now does the same thing as test.

* Fixing lint errors. The diff looks large but is large done through `yarn run lint --fix`

The only noteworthy change is the one in eslintrc for tests. The env has been updated from mocha to jest.

* Adding eslint-plugin-jest and further modify tests.

- One small fix in sqllab's Timer Spec for a test that is not using the spy it created for testing.
- Deletion of a duplicated test caught by eslint-plugin-jest.

* - Make istanbul coverage work with Jest.

- Remove dependency on stand-alone istanbul and babel-istanbul as they're built-into jest. Yes!

* Attempt to fix dynamic imports in tests.

* run sequentially and log heap usage

* - tweaking maxworkers for travis and specifying coverageDirectory for codecov

- remove dynamic import in shim.js now that it is set in babelrc for tests only.
2018-10-15 13:10:18 -07:00

150 lines
6.0 KiB
JavaScript

import * as r from '../../../src/SqlLab/reducers';
import * as actions from '../../../src/SqlLab/actions';
import { table, initialState as mockState } from './fixtures';
const initialState = mockState.sqlLab;
describe('sqlLabReducer', () => {
describe('CLONE_QUERY_TO_NEW_TAB', () => {
const testQuery = { sql: 'SELECT * FROM...', dbId: 1, id: 'flasj233' };
let newState = {
...initialState,
queries: { [testQuery.id]: testQuery },
};
beforeEach(() => {
newState = r.sqlLabReducer(newState, actions.cloneQueryToNewTab(testQuery));
});
it('should have at most one more tab', () => {
expect(newState.queryEditors).toHaveLength(2);
});
it('should have the same SQL as the cloned query', () => {
expect(newState.queryEditors[1].sql).toBe(testQuery.sql);
});
it('should prefix the new tab title with "Copy of"', () => {
expect(newState.queryEditors[1].title).toContain('Copy of');
});
it('should push the cloned tab onto tab history stack', () => {
expect(newState.tabHistory[1]).toBe(newState.queryEditors[1].id);
});
});
describe('Query editors actions', () => {
let newState;
let defaultQueryEditor;
let qe;
beforeEach(() => {
newState = { ...initialState };
defaultQueryEditor = newState.queryEditors[0];
qe = Object.assign({}, defaultQueryEditor);
newState = r.sqlLabReducer(newState, actions.addQueryEditor(qe));
qe = newState.queryEditors[newState.queryEditors.length - 1];
});
it('should add a query editor', () => {
expect(newState.queryEditors).toHaveLength(2);
});
it('should remove a query editor', () => {
expect(newState.queryEditors).toHaveLength(2);
newState = r.sqlLabReducer(newState, actions.removeQueryEditor(qe));
expect(newState.queryEditors).toHaveLength(1);
});
it('should set q query editor active', () => {
newState = r.sqlLabReducer(newState, actions.addQueryEditor(qe));
newState = r.sqlLabReducer(newState, actions.setActiveQueryEditor(defaultQueryEditor));
expect(newState.tabHistory[newState.tabHistory.length - 1]).toBe(defaultQueryEditor.id);
});
it('should not fail while setting DB', () => {
const dbId = 9;
newState = r.sqlLabReducer(newState, actions.queryEditorSetDb(qe, dbId));
expect(newState.queryEditors[1].dbId).toBe(dbId);
});
it('should not fail while setting schema', () => {
const schema = 'foo';
newState = r.sqlLabReducer(newState, actions.queryEditorSetSchema(qe, schema));
expect(newState.queryEditors[1].schema).toBe(schema);
});
it('should not fail while setting autorun ', () => {
newState = r.sqlLabReducer(newState, actions.queryEditorSetAutorun(qe, false));
expect(newState.queryEditors[1].autorun).toBe(false);
newState = r.sqlLabReducer(newState, actions.queryEditorSetAutorun(qe, true));
expect(newState.queryEditors[1].autorun).toBe(true);
});
it('should not fail while setting title', () => {
const title = 'a new title';
newState = r.sqlLabReducer(newState, actions.queryEditorSetTitle(qe, title));
expect(newState.queryEditors[1].title).toBe(title);
});
it('should not fail while setting Sql', () => {
const sql = 'SELECT nothing from dev_null';
newState = r.sqlLabReducer(newState, actions.queryEditorSetSql(qe, sql));
expect(newState.queryEditors[1].sql).toBe(sql);
});
it('should set selectedText', () => {
const selectedText = 'TEST';
expect(newState.queryEditors[0].selectedText).toBeNull();
newState = r.sqlLabReducer(
newState, actions.queryEditorSetSelectedText(newState.queryEditors[0], 'TEST'));
expect(newState.queryEditors[0].selectedText).toBe(selectedText);
});
});
describe('Tables', () => {
let newState;
let newTable;
beforeEach(() => {
newTable = Object.assign({}, table);
newState = r.sqlLabReducer(initialState, actions.mergeTable(newTable));
newTable = newState.tables[0];
});
it('should add a table', () => {
// Testing that beforeEach actually added the table
expect(newState.tables).toHaveLength(1);
});
it('should merge the table attributes', () => {
// Merging the extra attribute
newTable.extra = true;
newState = r.sqlLabReducer(newState, actions.mergeTable(newTable));
expect(newState.tables).toHaveLength(1);
expect(newState.tables[0].extra).toBe(true);
});
it('should expand and collapse a table', () => {
newState = r.sqlLabReducer(newState, actions.collapseTable(newTable));
expect(newState.tables[0].expanded).toBe(false);
newState = r.sqlLabReducer(newState, actions.expandTable(newTable));
expect(newState.tables[0].expanded).toBe(true);
});
it('should remove a table', () => {
newState = r.sqlLabReducer(newState, actions.removeTable(newTable));
expect(newState.tables).toHaveLength(0);
});
});
describe('Run Query', () => {
let newState;
let query;
let newQuery;
beforeEach(() => {
newState = { ...initialState };
newQuery = { ...query };
});
it('should start a query', () => {
newState = r.sqlLabReducer(newState, actions.startQuery(newQuery));
expect(Object.keys(newState.queries)).toHaveLength(1);
});
it('should stop the query', () => {
newState = r.sqlLabReducer(newState, actions.startQuery(newQuery));
newState = r.sqlLabReducer(newState, actions.stopQuery(newQuery));
const q = newState.queries[Object.keys(newState.queries)[0]];
expect(q.state).toBe('stopped');
});
it('should remove a query', () => {
newState = r.sqlLabReducer(newState, actions.startQuery(newQuery));
newState = r.sqlLabReducer(newState, actions.removeQuery(newQuery));
expect(Object.keys(newState.queries)).toHaveLength(0);
});
it('should refresh queries when polling returns empty', () => {
newState = r.sqlLabReducer(newState, actions.refreshQueries({}));
});
});
});