summaryrefslogtreecommitdiff
path: root/spec/frontend/logs/components/log_advanced_filters_spec.js
blob: dfa8913a301f359c29a9789fb89a789b161fe544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { shallowMount } from '@vue/test-utils';
import { GlFilteredSearch } from '@gitlab/ui';
import { defaultTimeRange } from '~/vue_shared/constants';
import { convertToFixedRange } from '~/lib/utils/datetime_range';
import { createStore } from '~/logs/stores';
import { TOKEN_TYPE_POD_NAME } from '~/logs/constants';
import { mockPods, mockSearch } from '../mock_data';

import LogAdvancedFilters from '~/logs/components/log_advanced_filters.vue';

const module = 'environmentLogs';

describe('LogAdvancedFilters', () => {
  let store;
  let dispatch;
  let wrapper;
  let state;

  const findFilteredSearch = () => wrapper.find(GlFilteredSearch);
  const findTimeRangePicker = () => wrapper.find({ ref: 'dateTimePicker' });
  const getSearchToken = (type) =>
    findFilteredSearch()
      .props('availableTokens')
      .filter((token) => token.type === type)[0];

  const mockStateLoading = () => {
    state.timeRange.selected = defaultTimeRange;
    state.timeRange.current = convertToFixedRange(defaultTimeRange);
    state.pods.options = [];
    state.pods.current = null;
    state.logs.isLoading = true;
  };

  const mockStateWithData = () => {
    state.timeRange.selected = defaultTimeRange;
    state.timeRange.current = convertToFixedRange(defaultTimeRange);
    state.pods.options = mockPods;
    state.pods.current = null;
    state.logs.isLoading = false;
  };

  const initWrapper = (propsData = {}) => {
    wrapper = shallowMount(LogAdvancedFilters, {
      propsData: {
        ...propsData,
      },
      store,
    });
  };

  beforeEach(() => {
    store = createStore();
    state = store.state.environmentLogs;

    jest.spyOn(store, 'dispatch').mockResolvedValue();

    dispatch = store.dispatch;
  });

  afterEach(() => {
    store.dispatch.mockReset();

    if (wrapper) {
      wrapper.destroy();
    }
  });

  it('displays UI elements', () => {
    initWrapper();

    expect(findFilteredSearch().exists()).toBe(true);
    expect(findTimeRangePicker().exists()).toBe(true);
  });

  it('displays search tokens', () => {
    initWrapper();

    expect(getSearchToken(TOKEN_TYPE_POD_NAME)).toMatchObject({
      title: 'Pod name',
      unique: true,
      operators: [expect.objectContaining({ value: '=' })],
    });
  });

  describe('disabled state', () => {
    beforeEach(() => {
      mockStateLoading();
      initWrapper({
        disabled: true,
      });
    });

    it('displays disabled filters', () => {
      expect(findFilteredSearch().attributes('disabled')).toBeTruthy();
      expect(findTimeRangePicker().attributes('disabled')).toBeTruthy();
    });
  });

  describe('when the state is loading', () => {
    beforeEach(() => {
      mockStateLoading();
      initWrapper();
    });

    it('displays a disabled search', () => {
      expect(findFilteredSearch().attributes('disabled')).toBeTruthy();
    });

    it('displays an enable date filter', () => {
      expect(findTimeRangePicker().attributes('disabled')).toBeFalsy();
    });

    it('displays no pod options when no pods are available, so suggestions can be displayed', () => {
      expect(getSearchToken(TOKEN_TYPE_POD_NAME).options).toBe(null);
      expect(getSearchToken(TOKEN_TYPE_POD_NAME).loading).toBe(true);
    });
  });

  describe('when the state has data', () => {
    beforeEach(() => {
      mockStateWithData();
      initWrapper();
    });

    it('displays a single token for pods', () => {
      initWrapper();

      const tokens = findFilteredSearch().props('availableTokens');

      expect(tokens).toHaveLength(1);
      expect(tokens[0].type).toBe(TOKEN_TYPE_POD_NAME);
    });

    it('displays a enabled filters', () => {
      expect(findFilteredSearch().attributes('disabled')).toBeFalsy();
      expect(findTimeRangePicker().attributes('disabled')).toBeFalsy();
    });

    it('displays options in the pods token', () => {
      const { options } = getSearchToken(TOKEN_TYPE_POD_NAME);

      expect(options).toHaveLength(mockPods.length);
    });

    it('displays options in date time picker', () => {
      const options = findTimeRangePicker().props('options');

      expect(options).toEqual(expect.any(Array));
      expect(options.length).toBeGreaterThan(0);
    });

    describe('when the user interacts', () => {
      it('clicks on the search button, showFilteredLogs is dispatched', () => {
        findFilteredSearch().vm.$emit('submit', null);

        expect(dispatch).toHaveBeenCalledWith(`${module}/showFilteredLogs`, null);
      });

      it('clicks on the search button, showFilteredLogs is dispatched with null', () => {
        findFilteredSearch().vm.$emit('submit', [mockSearch]);

        expect(dispatch).toHaveBeenCalledWith(`${module}/showFilteredLogs`, [mockSearch]);
      });

      it('selects a new time range', () => {
        expect(findTimeRangePicker().attributes('disabled')).toBeFalsy();

        const mockRange = { start: 'START_DATE', end: 'END_DATE' };
        findTimeRangePicker().vm.$emit('input', mockRange);

        expect(dispatch).toHaveBeenCalledWith(`${module}/setTimeRange`, mockRange);
      });
    });
  });
});