Skip to content

Commit

Permalink
Convert historian test to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
anmic committed Jul 16, 2019
1 parent 8b57080 commit 6943d3a
Showing 1 changed file with 51 additions and 55 deletions.
106 changes: 51 additions & 55 deletions test/unit/historian_test.ts → tests/historian.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import * as sinon from 'sinon';
import Client from '../../src/client';
import { expect } from './sinon_chai';
import Client from '../src/client';

class Location {
private s: string;
constructor(s) {
this.s = s;
}

constructor(s: string) {
this.s = s;
}

public toString(): string {
return this.s;
}
toString() {
return this.s;
}
}

describe('instrumentation', () => {
Expand All @@ -20,10 +16,10 @@ describe('instrumentation', () => {
let client;

beforeEach(() => {
processor = sinon.spy((data) => {
processor = jest.fn((data) => {
return data;
});
reporter = sinon.spy(() => {
reporter = jest.fn(() => {
return Promise.resolve({ id: 1 });
});
client = new Client({
Expand All @@ -39,7 +35,7 @@ describe('instrumentation', () => {
let locations = ['', 'http://hello/world', 'foo', new Location('/')];
for (let loc of locations) {
try {
window.history.pushState(null, '', loc as string);
window.history.pushState(null, '', loc);
} catch (_) {
// ignore
}
Expand All @@ -48,29 +44,29 @@ describe('instrumentation', () => {
});

it('records browser history', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 3];
delete state.date;
expect(state).to.deep.equal({
expect(state).toStrictEqual({
type: 'location',
from: '/context.html',
from: '/',
to: '/world',
});

state = history[history.length - 2];
delete state.date;
expect(state).to.deep.equal({
expect(state).toStrictEqual({
type: 'location',
from: '/world',
to: '/foo',
});

state = history[history.length - 1];
delete state.date;
expect(state).to.deep.equal({
expect(state).toStrictEqual({
type: 'location',
from: '/foo',
to: '/',
Expand All @@ -87,16 +83,16 @@ describe('instrumentation', () => {
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('GET');
expect(state.url).to.equal('http://ip2c.org/self');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('GET');
expect(state.url).toBe('http://ip2c.org/self');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});

Expand All @@ -111,16 +107,16 @@ describe('instrumentation', () => {
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('GET');
expect(state.url).to.equal('http://ip2c.org/4.4.4.4');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('GET');
expect(state.url).toBe('http://ip2c.org/4.4.4.4');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});

Expand All @@ -134,16 +130,16 @@ describe('instrumentation', () => {
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('POST');
expect(state.url).to.equal('http://ip2c.org/4.4.4.4');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('POST');
expect(state.url).toBe('http://ip2c.org/4.4.4.4');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});

Expand All @@ -161,16 +157,16 @@ describe('instrumentation', () => {
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('POST');
expect(state.url).to.equal('http://ip2c.org/4.4.4.4');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('POST');
expect(state.url).toBe('http://ip2c.org/4.4.4.4');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});
});
Expand All @@ -185,20 +181,20 @@ describe('instrumentation', () => {
});

it('records log message', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;
expect(history).to.have.length(20);
expect(history).toHaveLength(20);

for (let i in history) {
if (!history.hasOwnProperty(i)) {
continue;
}
let state = history[i];
expect(state.type).to.equal('log');
expect(state.severity).to.equal('log');
expect(state.arguments).to.deep.equal([+i + 5]);
expect(state.date).to.exist;
expect(state.type).toBe('log');
expect(state.severity).toBe('log');
expect(state.arguments).toStrictEqual([+i + 5]);
expect(state.date).not.toBeNull();
}
});
});
Expand Down

0 comments on commit 6943d3a

Please sign in to comment.