Welcome to mirror list, hosted at ThFree Co, Russian Federation.

log_simple_filters_spec.js « components « logs « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b819f0d25a89d1ff3b28eeef42dec7398c753878 (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
import { GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { createStore } from '~/logs/stores';
import { mockPods, mockPodName } from '../mock_data';

import LogSimpleFilters from '~/logs/components/log_simple_filters.vue';

const module = 'environmentLogs';

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

  const findPodsDropdown = () => wrapper.find({ ref: 'podsDropdown' });
  const findPodsNoPodsText = () => wrapper.find({ ref: 'noPodsMsg' });
  const findPodsDropdownItems = () =>
    findPodsDropdown()
      .findAll(GlDropdownItem)
      .filter(item => !('disabled' in item.attributes()));

  const mockPodsLoading = () => {
    state.pods.options = [];
    state.pods.current = null;
  };

  const mockPodsLoaded = () => {
    state.pods.options = mockPods;
    state.pods.current = mockPodName;
  };

  const initWrapper = (propsData = {}) => {
    wrapper = shallowMount(LogSimpleFilters, {
      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(findPodsDropdown().exists()).toBe(true);
  });

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

    it('displays a disabled pods dropdown', () => {
      expect(findPodsDropdown().props('text')).toBe('No pod selected');
      expect(findPodsDropdown().attributes('disabled')).toBeTruthy();
    });
  });

  describe('loading state', () => {
    beforeEach(() => {
      mockPodsLoading();
      initWrapper();
    });

    it('displays an enabled pods dropdown', () => {
      expect(findPodsDropdown().attributes('disabled')).toBeFalsy();
      expect(findPodsDropdown().props('text')).toBe('No pod selected');
    });

    it('displays an empty pods dropdown', () => {
      expect(findPodsNoPodsText().exists()).toBe(true);
      expect(findPodsDropdownItems()).toHaveLength(0);
    });
  });

  describe('pods available state', () => {
    beforeEach(() => {
      mockPodsLoaded();
      initWrapper();
    });

    it('displays an enabled pods dropdown', () => {
      expect(findPodsDropdown().attributes('disabled')).toBeFalsy();
      expect(findPodsDropdown().props('text')).toBe(mockPods[0]);
    });

    it('displays a pods dropdown with items', () => {
      expect(findPodsNoPodsText().exists()).toBe(false);
      expect(findPodsDropdownItems()).toHaveLength(mockPods.length);
    });

    it('dropdown has one pod selected', () => {
      const items = findPodsDropdownItems();
      mockPods.forEach((pod, i) => {
        const item = items.at(i);
        if (item.text() !== mockPodName) {
          expect(item.find(GlDropdownItem).attributes('ischecked')).toBeFalsy();
        } else {
          expect(item.find(GlDropdownItem).attributes('ischecked')).toBeTruthy();
        }
      });
    });

    it('when the user clicks on a pod, showPodLogs is dispatched', () => {
      const items = findPodsDropdownItems();
      const index = 2; // any pod

      expect(dispatch).not.toHaveBeenCalledWith(`${module}/showPodLogs`, expect.anything());

      items.at(index).vm.$emit('click');

      expect(dispatch).toHaveBeenCalledWith(`${module}/showPodLogs`, mockPods[index]);
    });
  });
});