123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- import {mount} from 'enzyme';
- import {
- getChartData,
- getChartDataWithPercentages,
- getChartDataByDay,
- getDisplayValue,
- getDisplayText,
- downloadAsCsv,
- } from 'app/views/organizationDiscover/result/utils';
- describe('Utils', function() {
- describe('getChartData()', function() {
- const raw = [
- {count: 2, uniq_id: 1, 'project.id': 5, environment: null},
- {count: 2, uniq_id: 3, 'project.id': 5, environment: 'staging'},
- {count: 2, uniq_id: 4, 'project.id': 5, environment: 'alpha'},
- {count: 6, uniq_id: 10, 'project.id': 5, environment: 'production'},
- ];
- const query = {
- aggregations: [['count()', null, 'count'], ['uniq', 'id', 'uniq_id']],
- fields: ['project.id', 'environment'],
- };
- it('returns chart data', function() {
- const expected = [
- {
- seriesName: 'count',
- data: [
- {value: 2, name: 'project.id 5 environment null'},
- {value: 2, name: 'project.id 5 environment staging'},
- {value: 2, name: 'project.id 5 environment alpha'},
- {value: 6, name: 'project.id 5 environment production'},
- ],
- },
- {
- seriesName: 'uniq_id',
- data: [
- {value: 1, name: 'project.id 5 environment null'},
- {value: 3, name: 'project.id 5 environment staging'},
- {value: 4, name: 'project.id 5 environment alpha'},
- {value: 10, name: 'project.id 5 environment production'},
- ],
- },
- ];
- expect(getChartData(raw, query)).toEqual(expected);
- });
- });
- describe('getChartDataWithPercentages()', function() {
- const raw = [
- {count: 2, uniq_id: 1, 'project.id': 5, environment: null},
- {count: 2, uniq_id: 3, 'project.id': 5, environment: 'staging'},
- {count: 2, uniq_id: 4, 'project.id': 5, environment: 'alpha'},
- {count: 6, uniq_id: 10, 'project.id': 5, environment: 'production'},
- ];
- const query = {
- aggregations: [['count()', null, 'count'], ['uniq', 'id', 'uniq_id']],
- fields: ['project.id', 'environment'],
- };
- it('returns chart data with percentages', function() {
- const expected = [
- {
- seriesName: 'count',
- data: [
- {value: 2, percentage: 16.67, name: '5 null'},
- {value: 2, percentage: 16.67, name: '5 staging'},
- {value: 2, percentage: 16.67, name: '5 alpha'},
- {value: 6, percentage: 50, name: '5 production'},
- ],
- },
- {
- seriesName: 'uniq_id',
- data: [
- {value: 1, percentage: 5.56, name: '5 null'},
- {value: 3, percentage: 16.67, name: '5 staging'},
- {value: 4, percentage: 22.22, name: '5 alpha'},
- {value: 10, percentage: 55.56, name: '5 production'},
- ],
- },
- ];
- expect(getChartDataWithPercentages(raw, query)).toEqual(expected);
- });
- });
- describe('getChartDataByDay()', function() {
- const raw = [
- {
- 'error.type': 'Type Error',
- platform: 'javascript',
- count: 5,
- time: 1532070000,
- },
- {
- 'error.type': 'Exception',
- platform: 'php',
- count: 8,
- time: 1532070000,
- },
- {
- 'error.type': 'SnubaError',
- platform: 'python',
- count: 30,
- time: 1532070000,
- },
- {
- 'error.type': 'ZeroDivisionError',
- platform: 'python',
- count: 20,
- time: 1531180800,
- },
- {
- 'error.type': 'ZeroDivisionError',
- platform: 'python',
- count: 6,
- time: 1531094400,
- },
- {
- 'error.type': 'Type Error',
- platform: 'javascript',
- count: 6,
- time: 1531094400,
- },
- {
- 'error.type': 'Exception',
- platform: 'php',
- count: 6,
- time: 1531094400,
- },
- {
- 'error.type': 'SnubaError',
- platform: 'python',
- count: 14,
- time: 1531094400,
- },
- ];
- const query = {
- aggregations: [['count()', null, 'count']],
- fields: ['platform', 'error.type'],
- };
- it('returns chart data grouped by day', function() {
- const expected = [
- {
- data: [
- {name: 1531094400000, value: 14},
- {name: 1531180800000, value: null},
- {name: 1532070000000, value: 30},
- ],
- seriesName: 'python,SnubaError',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: null},
- {name: 1532070000000, value: 8},
- ],
- seriesName: 'php,Exception',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: null},
- {name: 1532070000000, value: 5},
- ],
- seriesName: 'javascript,Type Error',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: 20},
- {name: 1532070000000, value: null},
- ],
- seriesName: 'python,ZeroDivisionError',
- },
- ];
- expect(getChartDataByDay(raw, query)).toEqual(expected);
- });
- it('assumes null value as 0', function() {
- const expected = [
- {
- data: [
- {name: 1531094400000, value: 14},
- {name: 1531180800000, value: 0},
- {name: 1532070000000, value: 30},
- ],
- seriesName: 'python,SnubaError',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: 0},
- {name: 1532070000000, value: 8},
- ],
- seriesName: 'php,Exception',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: 0},
- {name: 1532070000000, value: 5},
- ],
- seriesName: 'javascript,Type Error',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: 20},
- {name: 1532070000000, value: 0},
- ],
- seriesName: 'python,ZeroDivisionError',
- },
- ];
- expect(getChartDataByDay(raw, query, {assumeNullAsZero: true})).toEqual(expected);
- });
- it('shows only top 10 series by default', function() {
- expect(
- getChartDataByDay(
- [
- ...raw,
- ...[...new Array(10)].map(() => ({
- 'error.type': 'Exeption',
- platform: `${Math.random()}`,
- count: 10,
- time: 1532070000,
- })),
- ],
- query
- )
- ).toHaveLength(10);
- });
- it('shows all series', function() {
- expect(
- getChartDataByDay(
- [
- ...raw,
- ...[...new Array(10)].map(() => ({
- 'error.type': 'Exeption',
- platform: `${Math.random()}`,
- count: 10,
- time: 1532070000,
- })),
- ],
- query,
- {allSeries: true}
- )
- ).toHaveLength(14);
- });
- it('maps field value to label', function() {
- const expected = [
- {
- data: [
- {name: 1531094400000, value: 14},
- {name: 1531180800000, value: null},
- {name: 1532070000000, value: 30},
- ],
- seriesName: 'SNAKES,SnubaError',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: null},
- {name: 1532070000000, value: 8},
- ],
- seriesName: 'PHP,Exception',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: null},
- {name: 1532070000000, value: 5},
- ],
- seriesName: 'NOT JAVA,Type Error',
- },
- {
- data: [
- {name: 1531094400000, value: 6},
- {name: 1531180800000, value: 20},
- {name: 1532070000000, value: null},
- ],
- seriesName: 'SNAKES,ZeroDivisionError',
- },
- ];
- expect(
- getChartDataByDay(raw, query, {
- fieldLabelMap: {python: 'SNAKES', php: 'PHP', javascript: 'NOT JAVA'},
- })
- ).toEqual(expected);
- });
- });
- it('getDisplayValue()', function() {
- const testData = [
- {input: null, expectedText: 'null'},
- {
- input: 'some thing',
- expectedText: '"some thing"',
- },
- {
- input: 12,
- expectedText: '12',
- },
- {
- input: ['one', 'two', 'three'],
- expectedText: '["one","two","three"]',
- },
- {
- input: 1000000,
- expectedText: '1,000,000',
- },
- ];
- testData.forEach(({input, expectedText}) => {
- expect(mount(getDisplayValue(input)).text()).toBe(expectedText);
- });
- });
- it('getTextValue()', function() {
- const testData = [
- {input: null, expectedText: 'null'},
- {
- input: 'some thing',
- expectedText: '"some thing"',
- },
- {
- input: 12,
- expectedText: '12',
- },
- {
- input: ['one', 'two', 'three'],
- expectedText: '["one","two","three"]',
- },
- {
- input: 1000000,
- expectedText: '1,000,000',
- },
- ];
- testData.forEach(({input, expectedText}) => {
- expect(getDisplayText(input)).toBe(expectedText);
- });
- });
- describe('downloadAsCsv()', function() {
- let locationSpy;
- beforeEach(function() {
- locationSpy = jest.spyOn(window.location, 'assign').mockImplementation(_ => _);
- });
- afterEach(function() {
- jest.restoreAllMocks();
- });
- it('handles raw data', function() {
- const result = {
- meta: [{name: 'message'}, {name: 'environment'}],
- data: [
- {message: 'test 1', environment: 'prod'},
- {message: 'test 2', environment: 'test'},
- ],
- };
- downloadAsCsv(result);
- expect(locationSpy).toHaveBeenCalledWith(
- expect.stringContaining(
- encodeURI('message,environment\r\ntest 1,prod\r\ntest 2,test')
- )
- );
- });
- it('handles aggregations', function() {
- const result = {
- meta: [{type: 'UInt64', name: 'count'}],
- data: [{count: 3}],
- };
- downloadAsCsv(result);
- expect(locationSpy).toHaveBeenCalledWith(
- expect.stringContaining(encodeURI('count\r\n3'))
- );
- });
- it('quotes unsafe strings', function() {
- const result = {
- meta: [{name: 'message'}],
- data: [{message: '=HYPERLINK(http://some-bad-website)'}],
- };
- downloadAsCsv(result);
- expect(locationSpy).toHaveBeenCalledWith(
- expect.stringContaining(
- encodeURI("message\r\n'=HYPERLINK(http://some-bad-website)")
- )
- );
- });
- });
- });
|