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

confirm_danger_modal_spec.js « confirm_danger « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b825a578ceed78a9850052bf8e0be28c8a448303 (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
import { GlModal, GlSprintf } from '@gitlab/ui';
import {
  CONFIRM_DANGER_WARNING,
  CONFIRM_DANGER_MODAL_BUTTON,
  CONFIRM_DANGER_MODAL_ID,
  CONFIRM_DANGER_MODAL_CANCEL,
} from '~/vue_shared/components/confirm_danger/constants';
import ConfirmDangerModal from '~/vue_shared/components/confirm_danger/confirm_danger_modal.vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';

describe('Confirm Danger Modal', () => {
  const confirmDangerMessage = 'This is a dangerous activity';
  const confirmButtonText = 'Confirm button text';
  const cancelButtonText = 'Cancel button text';
  const phrase = 'You must construct additional pylons';
  const modalId = CONFIRM_DANGER_MODAL_ID;

  let wrapper;

  const findModal = () => wrapper.findComponent(GlModal);
  const findConfirmationPhrase = () => wrapper.findByTestId('confirm-danger-phrase');
  const findConfirmationInput = () => wrapper.findByTestId('confirm-danger-field');
  const findDefaultWarning = () => wrapper.findByTestId('confirm-danger-warning');
  const findAdditionalMessage = () => wrapper.findByTestId('confirm-danger-message');
  const findPrimaryAction = () => findModal().props('actionPrimary');
  const findCancelAction = () => findModal().props('actionCancel');
  const findPrimaryActionAttributes = (attr) => findPrimaryAction().attributes[attr];

  const createComponent = ({ provide = {} } = {}) =>
    shallowMountExtended(ConfirmDangerModal, {
      propsData: {
        modalId,
        phrase,
        visible: false,
      },
      provide,
      stubs: { GlSprintf },
    });

  beforeEach(() => {
    wrapper = createComponent({
      provide: { confirmDangerMessage, confirmButtonText, cancelButtonText },
    });
  });

  it('renders the default warning message', () => {
    expect(findDefaultWarning().text()).toBe(CONFIRM_DANGER_WARNING);
  });

  it('renders any additional messages', () => {
    expect(findAdditionalMessage().text()).toBe(confirmDangerMessage);
  });

  it('renders the confirm button', () => {
    expect(findPrimaryAction().text).toBe(confirmButtonText);
    expect(findPrimaryActionAttributes('variant')).toBe('danger');
  });

  it('renders the cancel button', () => {
    expect(findCancelAction().text).toBe(cancelButtonText);
  });

  it('renders the correct confirmation phrase', () => {
    expect(findConfirmationPhrase().text()).toBe(`Please type ${phrase} to proceed.`);
  });

  describe('without injected data', () => {
    beforeEach(() => {
      wrapper = createComponent();
    });

    it('does not render any additional messages', () => {
      expect(findAdditionalMessage().exists()).toBe(false);
    });

    it('renders the default confirm button', () => {
      expect(findPrimaryAction().text).toBe(CONFIRM_DANGER_MODAL_BUTTON);
    });

    it('renders the default cancel button', () => {
      expect(findCancelAction().text).toBe(CONFIRM_DANGER_MODAL_CANCEL);
    });
  });

  describe('with a valid confirmation phrase', () => {
    beforeEach(() => {
      wrapper = createComponent();
    });

    it('enables the confirm button', async () => {
      expect(findPrimaryActionAttributes('disabled')).toBe(true);

      await findConfirmationInput().vm.$emit('input', phrase);

      expect(findPrimaryActionAttributes('disabled')).toBe(false);
    });

    it('emits a `confirm` event when the button is clicked', async () => {
      expect(wrapper.emitted('confirm')).toBeUndefined();

      await findConfirmationInput().vm.$emit('input', phrase);
      await findModal().vm.$emit('primary');

      expect(wrapper.emitted('confirm')).not.toBeUndefined();
    });
  });

  describe('v-model', () => {
    it('emit `change` event', () => {
      findModal().vm.$emit('change', true);

      expect(wrapper.emitted('change')).toEqual([[true]]);
    });

    it('sets `visible` prop', () => {
      expect(findModal().props('visible')).toBe(false);
    });
  });
});