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

password_prompt_modal_spec.js « password_prompt « profiles « pages « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b722ac1e97b3851824f30eaa94a8554204724a4c (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
import { GlModal } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import {
  I18N_PASSWORD_PROMPT_CANCEL_BUTTON,
  I18N_PASSWORD_PROMPT_CONFIRM_BUTTON,
} from '~/pages/profiles/password_prompt/constants';
import PasswordPromptModal from '~/pages/profiles/password_prompt/password_prompt_modal.vue';

const createComponent = ({ props }) => {
  return shallowMountExtended(PasswordPromptModal, {
    propsData: {
      ...props,
    },
  });
};

describe('Password prompt modal', () => {
  let wrapper;

  const mockPassword = 'not+fake+shady+password';
  const mockEvent = { preventDefault: jest.fn() };
  const handleConfirmPasswordSpy = jest.fn();

  const findField = () => wrapper.findByTestId('password-prompt-field');
  const findModal = () => wrapper.findComponent(GlModal);
  const findConfirmBtn = () => findModal().props('actionPrimary');
  const findConfirmBtnDisabledState = () =>
    findModal().props('actionPrimary').attributes[2].disabled;

  const findCancelBtn = () => findModal().props('actionCancel');

  const submitModal = () => findModal().vm.$emit('primary', mockEvent);
  const setPassword = (newPw) => findField().vm.$emit('input', newPw);

  beforeEach(() => {
    wrapper = createComponent({
      props: {
        handleConfirmPassword: handleConfirmPasswordSpy,
      },
    });
  });

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

  it('renders the password field', () => {
    expect(findField().exists()).toBe(true);
  });

  it('renders the confirm button', () => {
    expect(findConfirmBtn().text).toEqual(I18N_PASSWORD_PROMPT_CONFIRM_BUTTON);
  });

  it('renders the cancel button', () => {
    expect(findCancelBtn().text).toEqual(I18N_PASSWORD_PROMPT_CANCEL_BUTTON);
  });

  describe('confirm button', () => {
    describe('with a valid password', () => {
      it('calls the `handleConfirmPassword` method when clicked', async () => {
        setPassword(mockPassword);
        submitModal();

        await wrapper.vm.$nextTick();

        expect(handleConfirmPasswordSpy).toHaveBeenCalledTimes(1);
        expect(handleConfirmPasswordSpy).toHaveBeenCalledWith(mockPassword);
      });

      it('enables the confirm button', async () => {
        setPassword(mockPassword);

        expect(findConfirmBtnDisabledState()).toBe(true);

        await wrapper.vm.$nextTick();

        expect(findConfirmBtnDisabledState()).toBe(false);
      });
    });

    it('without a valid password is disabled', async () => {
      setPassword('');

      expect(findConfirmBtnDisabledState()).toBe(true);

      await wrapper.vm.$nextTick();

      expect(findConfirmBtnDisabledState()).toBe(true);
    });
  });
});