Files
superset2/superset/assets/spec/javascripts/logger_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

151 lines
4.5 KiB
JavaScript

import $ from 'jquery';
import sinon from 'sinon';
import { Logger, ActionLog } from '../../src/logger';
describe('ActionLog', () => {
it('should be a constructor', () => {
const newLogger = new ActionLog({});
expect(newLogger instanceof ActionLog).toBe(true);
});
it(
'should set the eventNames, impressionId, source, sourceId, and sendNow init parameters',
() => {
const eventNames = [];
const impressionId = 'impressionId';
const source = 'source';
const sourceId = 'sourceId';
const sendNow = true;
const log = new ActionLog({ eventNames, impressionId, source, sourceId, sendNow });
expect(log.eventNames).toBe(eventNames);
expect(log.impressionId).toBe(impressionId);
expect(log.source).toBe(source);
expect(log.sourceId).toBe(sourceId);
expect(log.sendNow).toBe(sendNow);
},
);
it('should set attributes with the setAttribute method', () => {
const log = new ActionLog({});
expect(log.test).toBeUndefined();
log.setAttribute('test', 'testValue');
expect(log.test).toBe('testValue');
});
it('should track added events', () => {
const log = new ActionLog({});
const eventName = 'myEventName';
const eventBody = { test: 'event' };
expect(log.events[eventName]).toBeUndefined();
log.addEvent(eventName, eventBody);
expect(log.events[eventName]).toHaveLength(1);
expect(log.events[eventName][0]).toMatchObject(eventBody);
});
});
describe('Logger', () => {
it('should add events when .append(eventName, eventBody) is called', () => {
const eventName = 'testEvent';
const eventBody = { test: 'event' };
const log = new ActionLog({ eventNames: [eventName] });
Logger.start(log);
Logger.append(eventName, eventBody);
expect(log.events[eventName]).toHaveLength(1);
expect(log.events[eventName][0]).toMatchObject(eventBody);
Logger.end(log);
});
describe('.send()', () => {
beforeEach(() => {
sinon.spy($, 'ajax');
});
afterEach(() => {
$.ajax.restore();
});
const eventNames = ['test'];
function setup(overrides = {}) {
const log = new ActionLog({ eventNames, ...overrides });
return log;
}
it('should POST an event to /superset/log/ when called', () => {
const log = setup();
Logger.start(log);
Logger.append(eventNames[0], { test: 'event' });
expect(log.events[eventNames[0]]).toHaveLength(1);
Logger.end(log);
expect($.ajax.calledOnce).toBe(true);
const args = $.ajax.getCall(0).args[0];
expect(args.url).toBe('/superset/log/');
expect(args.method).toBe('POST');
});
it("should flush the log's events", () => {
const log = setup();
Logger.start(log);
Logger.append(eventNames[0], { test: 'event' });
const event = log.events[eventNames[0]][0];
expect(event).toMatchObject({ test: 'event' });
Logger.end(log);
expect(log.events).toEqual({});
});
it(
'should include ts, start_offset, event_name, impression_id, source, and source_id in every event',
() => {
const config = {
eventNames: ['event1', 'event2'],
impressionId: 'impress_me',
source: 'superset',
sourceId: 'lolz',
};
const log = setup(config);
Logger.start(log);
Logger.append('event1', { key: 'value' });
Logger.append('event2', { foo: 'bar' });
Logger.end(log);
const args = $.ajax.getCall(0).args[0];
const events = JSON.parse(args.data.events);
expect(events).toHaveLength(2);
expect(events[0]).toMatchObject({
key: 'value',
event_name: 'event1',
impression_id: config.impressionId,
source: config.source,
source_id: config.sourceId,
});
expect(events[1]).toMatchObject({
foo: 'bar',
event_name: 'event2',
impression_id: config.impressionId,
source: config.source,
source_id: config.sourceId,
});
expect(typeof events[0].ts).toBe('number');
expect(typeof events[1].ts).toBe('number');
expect(typeof events[0].start_offset).toBe('number');
expect(typeof events[1].start_offset).toBe('number');
},
);
it(
'should send() a log immediately if .append() is called with sendNow=true',
() => {
const log = setup();
Logger.start(log);
Logger.append(eventNames[0], { test: 'event' }, true);
expect($.ajax.calledOnce).toBe(true);
Logger.end(log);
},
);
});
});