debian-mirror-gitlab/spec/frontend/profile/add_ssh_key_validation_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
3 KiB
JavaScript
Raw Normal View History

2022-06-21 17:19:12 +05:30
import AddSshKeyValidation from '~/profile/add_ssh_key_validation';
2018-11-08 19:23:39 +05:30
describe('AddSshKeyValidation', () => {
describe('submit', () => {
it('returns true if isValid is true', () => {
2022-03-02 08:16:31 +05:30
const addSshKeyValidation = new AddSshKeyValidation([], {});
jest.spyOn(addSshKeyValidation, 'isPublicKey').mockReturnValue(true);
2018-11-08 19:23:39 +05:30
2022-03-02 08:16:31 +05:30
expect(addSshKeyValidation.submit()).toBe(true);
2018-11-08 19:23:39 +05:30
});
it('calls preventDefault and toggleWarning if isValid is false', () => {
2022-03-02 08:16:31 +05:30
const addSshKeyValidation = new AddSshKeyValidation([], {});
2020-03-13 15:44:24 +05:30
const event = {
preventDefault: jest.fn(),
};
2022-03-02 08:16:31 +05:30
jest.spyOn(addSshKeyValidation, 'isPublicKey').mockReturnValue(false);
2020-03-13 15:44:24 +05:30
jest.spyOn(addSshKeyValidation, 'toggleWarning').mockImplementation(() => {});
2018-11-08 19:23:39 +05:30
addSshKeyValidation.submit(event);
expect(event.preventDefault).toHaveBeenCalled();
expect(addSshKeyValidation.toggleWarning).toHaveBeenCalledWith(true);
});
});
describe('toggleWarning', () => {
it('shows warningElement and hides originalSubmitElement if isVisible is true', () => {
const warningElement = document.createElement('div');
const originalSubmitElement = document.createElement('div');
warningElement.classList.add('hide');
const addSshKeyValidation = new AddSshKeyValidation(
2022-03-02 08:16:31 +05:30
[],
2018-11-08 19:23:39 +05:30
{},
warningElement,
originalSubmitElement,
);
addSshKeyValidation.toggleWarning(true);
2022-03-02 08:16:31 +05:30
expect(warningElement.classList.contains('hide')).toBe(false);
expect(originalSubmitElement.classList.contains('hide')).toBe(true);
2018-11-08 19:23:39 +05:30
});
it('hides warningElement and shows originalSubmitElement if isVisible is false', () => {
const warningElement = document.createElement('div');
const originalSubmitElement = document.createElement('div');
originalSubmitElement.classList.add('hide');
const addSshKeyValidation = new AddSshKeyValidation(
2022-03-02 08:16:31 +05:30
[],
2018-11-08 19:23:39 +05:30
{},
warningElement,
originalSubmitElement,
);
addSshKeyValidation.toggleWarning(false);
2022-03-02 08:16:31 +05:30
expect(warningElement.classList.contains('hide')).toBe(true);
expect(originalSubmitElement.classList.contains('hide')).toBe(false);
2018-11-08 19:23:39 +05:30
});
});
describe('isPublicKey', () => {
2022-03-02 08:16:31 +05:30
it('returns false if value begins with an algorithm name that is unsupported', () => {
const addSshKeyValidation = new AddSshKeyValidation(['ssh-rsa', 'ssh-algorithm'], {});
expect(addSshKeyValidation.isPublicKey('nope key')).toBe(false);
expect(addSshKeyValidation.isPublicKey('ssh- key')).toBe(false);
expect(addSshKeyValidation.isPublicKey('unsupported-ssh-rsa key')).toBe(false);
2018-11-08 19:23:39 +05:30
});
2022-03-02 08:16:31 +05:30
it('returns true if value begins with an algorithm name that is supported', () => {
const addSshKeyValidation = new AddSshKeyValidation(['ssh-rsa', 'ssh-algorithm'], {});
expect(addSshKeyValidation.isPublicKey('ssh-rsa key')).toBe(true);
expect(addSshKeyValidation.isPublicKey('ssh-algorithm key')).toBe(true);
2018-11-08 19:23:39 +05:30
});
});
});