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

timezone_dropdown_spec.js « components « deploy_freeze « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc25c7647b848714a6d6d3a090887a4b3cc849f8 (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
import Vuex from 'vuex';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import { GlDeprecatedDropdownItem, GlDropdown } from '@gitlab/ui';
import { secondsToHours } from '~/lib/utils/datetime_utility';
import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue';
import createStore from '~/deploy_freeze/store';

const localVue = createLocalVue();
localVue.use(Vuex);

describe('Deploy freeze timezone dropdown', () => {
  let wrapper;
  let store;
  const timezoneDataFixture = getJSONFixture('/api/freeze-periods/timezone_data.json');

  const findTzByName = (identifier = '') =>
    timezoneDataFixture.find(({ name }) => name.toLowerCase() === identifier.toLowerCase());

  const formatTz = ({ offset, name }) => `[UTC ${secondsToHours(offset)}] ${name}`;

  const createComponent = (searchTerm, selectedTimezone) => {
    store = createStore({
      projectId: '8',
      timezoneData: timezoneDataFixture,
    });
    wrapper = shallowMount(TimezoneDropdown, {
      store,
      localVue,
      propsData: {
        value: selectedTimezone,
        timezoneData: timezoneDataFixture,
      },
    });

    wrapper.setData({ searchTerm });
  };

  const findAllDropdownItems = () => wrapper.findAll(GlDeprecatedDropdownItem);
  const findDropdownItemByIndex = index => wrapper.findAll(GlDeprecatedDropdownItem).at(index);

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('No time zones found', () => {
    beforeEach(() => {
      createComponent('UTC timezone');
    });

    it('renders empty results message', () => {
      expect(findDropdownItemByIndex(0).text()).toBe('No matching results');
    });
  });

  describe('Search term is empty', () => {
    beforeEach(() => {
      createComponent('');
    });

    it('renders all timezones when search term is empty', () => {
      expect(findAllDropdownItems()).toHaveLength(timezoneDataFixture.length);
    });
  });

  describe('Time zones found', () => {
    beforeEach(() => {
      createComponent('Alaska');
    });

    it('renders only the time zone searched for', () => {
      const selectedTz = findTzByName('Alaska');
      expect(findAllDropdownItems()).toHaveLength(1);
      expect(findDropdownItemByIndex(0).text()).toBe(formatTz(selectedTz));
    });

    it('should not display empty results message', () => {
      expect(wrapper.find('[data-testid="noMatchingResults"]').exists()).toBe(false);
    });

    describe('Custom events', () => {
      const selectedTz = findTzByName('Alaska');

      it('should emit input if a time zone is clicked', () => {
        findDropdownItemByIndex(0).vm.$emit('click');
        expect(wrapper.emitted('input')).toEqual([
          [
            {
              formattedTimezone: formatTz(selectedTz),
              identifier: selectedTz.identifier,
            },
          ],
        ]);
      });
    });
  });

  describe('Selected time zone', () => {
    beforeEach(() => {
      createComponent('', 'Alaska');
    });

    it('renders selected time zone as dropdown label', () => {
      expect(wrapper.find(GlDropdown).vm.text).toBe('Alaska');
    });
  });
});