summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/confirm_danger/confirm_danger_spec.js
blob: a179afccae0a2399ba8c4d79d26993b591e50538 (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
import { GlButton } from '@gitlab/ui';
import ConfirmDanger from '~/vue_shared/components/confirm_danger/confirm_danger.vue';
import ConfirmDangerModal from '~/vue_shared/components/confirm_danger/confirm_danger_modal.vue';
import { CONFIRM_DANGER_MODAL_ID } from '~/vue_shared/components/confirm_danger/constants';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';

describe('Confirm Danger Modal', () => {
  let wrapper;

  const phrase = 'En Taro Adun';
  const buttonText = 'Click me!';
  const buttonClass = 'gl-w-full';
  const buttonVariant = 'info';
  const modalId = CONFIRM_DANGER_MODAL_ID;

  const findBtn = () => wrapper.findComponent(GlButton);
  const findModal = () => wrapper.findComponent(ConfirmDangerModal);
  const findModalProps = () => findModal().props();

  const createComponent = (props = {}) =>
    shallowMountExtended(ConfirmDanger, {
      propsData: {
        buttonText,
        buttonClass,
        buttonVariant,
        phrase,
        ...props,
      },
    });

  beforeEach(() => {
    wrapper = createComponent();
  });

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

  it('renders the button', () => {
    expect(wrapper.html()).toContain(buttonText);
  });

  it('sets the modal properties', () => {
    expect(findModalProps()).toMatchObject({
      modalId,
      phrase,
    });
  });

  it('will disable the button if `disabled=true`', () => {
    expect(findBtn().attributes('disabled')).toBeUndefined();

    wrapper = createComponent({ disabled: true });

    expect(findBtn().attributes('disabled')).toBe('true');
  });

  it('passes `buttonClass` prop to button', () => {
    expect(findBtn().classes()).toContain(buttonClass);
  });

  it('passes `buttonVariant` prop to button', () => {
    expect(findBtn().attributes('variant')).toBe(buttonVariant);
  });

  it('will emit `confirm` when the modal confirms', () => {
    expect(wrapper.emitted('confirm')).toBeUndefined();

    findModal().vm.$emit('confirm');

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