123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- import {mount} from 'enzyme';
- import React from 'react';
- import {doEventsRequest} from 'app/actionCreators/events';
- import EventsRequest from 'app/views/events/utils/eventsRequest';
- const COUNT_OBJ = {
- count: 123,
- };
- jest.mock('app/actionCreators/events', () => {
- return {
- doEventsRequest: jest.fn(),
- };
- });
- describe('EventsRequest', function() {
- const project = TestStubs.Project();
- const organization = TestStubs.Organization();
- const mock = jest.fn(() => null);
- const DEFAULTS = {
- api: {},
- projects: [parseInt(project.id, 10)],
- environments: [],
- period: '24h',
- organization,
- tag: 'release',
- includePrevious: false,
- includeTimeseries: true,
- };
- let wrapper;
- describe('with props changes', function() {
- beforeAll(function() {
- doEventsRequest.mockImplementation(() =>
- Promise.resolve({
- data: [[new Date(), [COUNT_OBJ]]],
- })
- );
- wrapper = mount(<EventsRequest {...DEFAULTS}>{mock}</EventsRequest>);
- });
- it('makes requests', async function() {
- expect(mock).toHaveBeenNthCalledWith(
- 1,
- expect.objectContaining({
- loading: true,
- })
- );
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- loading: false,
- timeseriesData: [
- {
- seriesName: expect.anything(),
- data: [
- expect.objectContaining({
- name: expect.any(Number),
- value: 123,
- }),
- ],
- },
- ],
- originalTimeseriesData: [[expect.anything(), expect.anything()]],
- })
- );
- expect(doEventsRequest).toHaveBeenCalled();
- });
- it('makes a new request if projects prop changes', async function() {
- doEventsRequest.mockClear();
- wrapper.setProps({projects: [123]});
- await tick();
- wrapper.update();
- expect(doEventsRequest).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- projects: [123],
- })
- );
- });
- it('makes a new request if environments prop changes', async function() {
- doEventsRequest.mockClear();
- wrapper.setProps({environments: ['dev']});
- await tick();
- wrapper.update();
- expect(doEventsRequest).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- environments: ['dev'],
- })
- );
- });
- it('makes a new request if period prop changes', async function() {
- doEventsRequest.mockClear();
- wrapper.setProps({period: '7d'});
- await tick();
- wrapper.update();
- expect(doEventsRequest).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- period: '7d',
- })
- );
- });
- });
- describe('transforms', function() {
- beforeEach(function() {
- doEventsRequest.mockClear();
- });
- it('expands period in query if `includePrevious`', async function() {
- doEventsRequest.mockImplementation(() =>
- Promise.resolve({
- data: [
- [new Date(), [{...COUNT_OBJ, count: 321}, {...COUNT_OBJ, count: 79}]],
- [new Date(), [COUNT_OBJ]],
- ],
- })
- );
- wrapper = mount(
- <EventsRequest {...DEFAULTS} includePrevious={true}>
- {mock}
- </EventsRequest>
- );
- await tick();
- wrapper.update();
- // actionCreator handles expanding the period when calling the API
- expect(doEventsRequest).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- period: '24h',
- })
- );
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- loading: false,
- allTimeseriesData: [
- [
- expect.anything(),
- [
- expect.objectContaining({count: 321}),
- expect.objectContaining({count: 79}),
- ],
- ],
- [expect.anything(), [expect.objectContaining({count: 123})]],
- ],
- timeseriesData: [
- {
- seriesName: expect.anything(),
- data: [
- expect.objectContaining({
- name: expect.anything(),
- value: 123,
- }),
- ],
- },
- ],
- previousTimeseriesData: {
- seriesName: 'Previous Period',
- data: [
- expect.objectContaining({
- name: expect.anything(),
- value: 400,
- }),
- ],
- },
- originalTimeseriesData: [
- [expect.anything(), [expect.objectContaining({count: 123})]],
- ],
- originalPreviousTimeseriesData: [
- [
- expect.anything(),
- [
- expect.objectContaining({count: 321}),
- expect.objectContaining({count: 79}),
- ],
- ],
- ],
- })
- );
- });
- it('aggregates counts per timestamp only when `includeTimeAggregation` prop is true', async function() {
- doEventsRequest.mockImplementation(() =>
- Promise.resolve({
- data: [[new Date(), [COUNT_OBJ, {...COUNT_OBJ, count: 100}]]],
- })
- );
- wrapper = mount(
- <EventsRequest {...DEFAULTS} includeTimeseries={true}>
- {mock}
- </EventsRequest>
- );
- await tick();
- wrapper.update();
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- timeAggregatedData: {},
- })
- );
- wrapper.setProps({
- includeTimeAggregation: true,
- timeAggregationSeriesName: 'aggregated series',
- });
- await tick();
- wrapper.update();
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- timeAggregatedData: {
- seriesName: 'aggregated series',
- data: [{name: expect.anything(), value: 223}],
- },
- })
- );
- });
- it('aggregates all counts per timestamp when category name identical', async function() {
- doEventsRequest.mockImplementation(() =>
- Promise.resolve({
- data: [[new Date(), [COUNT_OBJ, {...COUNT_OBJ, count: 100}]]],
- })
- );
- wrapper = mount(
- <EventsRequest {...DEFAULTS} includeTimeseries={true}>
- {mock}
- </EventsRequest>
- );
- await tick();
- wrapper.update();
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- timeAggregatedData: {},
- })
- );
- wrapper.setProps({
- includeTimeAggregation: true,
- timeAggregationSeriesName: 'aggregated series',
- });
- await tick();
- wrapper.update();
- expect(mock).toHaveBeenLastCalledWith(
- expect.objectContaining({
- timeAggregatedData: {
- seriesName: 'aggregated series',
- data: [{name: expect.anything(), value: 223}],
- },
- })
- );
- });
- });
- });
|