2020-09-30 12:09:53 +00:00
|
|
|
import { shallowMount, createLocalVue } from '@vue/test-utils';
|
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
import createMockApollo from 'jest/helpers/mock_apollo_helper';
|
2020-08-17 21:09:56 +00:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2020-01-29 12:09:08 +00:00
|
|
|
import Tracking from '~/tracking';
|
2019-12-19 12:07:35 +00:00
|
|
|
import component from '~/registry/settings/components/settings_form.vue';
|
2020-02-04 12:09:00 +00:00
|
|
|
import expirationPolicyFields from '~/registry/shared/components/expiration_policy_fields.vue';
|
2020-09-30 12:09:53 +00:00
|
|
|
import updateContainerExpirationPolicyMutation from '~/registry/settings/graphql/mutations/update_container_expiration_policy.graphql';
|
|
|
|
import expirationPolicyQuery from '~/registry/settings/graphql/queries/get_expiration_policy.graphql';
|
2020-01-21 18:07:31 +00:00
|
|
|
import {
|
|
|
|
UPDATE_SETTINGS_ERROR_MESSAGE,
|
|
|
|
UPDATE_SETTINGS_SUCCESS_MESSAGE,
|
2020-02-03 09:08:42 +00:00
|
|
|
} from '~/registry/shared/constants';
|
2020-09-30 12:09:53 +00:00
|
|
|
import { GlCard, GlLoadingIcon } from '../../shared/stubs';
|
|
|
|
import { expirationPolicyPayload, expirationPolicyMutationPayload } from '../mock_data';
|
|
|
|
|
|
|
|
const localVue = createLocalVue();
|
2019-12-19 12:07:35 +00:00
|
|
|
|
|
|
|
describe('Settings Form', () => {
|
|
|
|
let wrapper;
|
2020-09-30 12:09:53 +00:00
|
|
|
let fakeApollo;
|
|
|
|
|
|
|
|
const defaultProvidedValues = {
|
|
|
|
projectPath: 'path',
|
|
|
|
};
|
|
|
|
|
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
project: { containerExpirationPolicy },
|
|
|
|
},
|
|
|
|
} = expirationPolicyPayload();
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
value: { ...containerExpirationPolicy },
|
2020-02-04 12:09:00 +00:00
|
|
|
};
|
|
|
|
|
2020-01-29 12:09:08 +00:00
|
|
|
const trackingPayload = {
|
|
|
|
label: 'docker_container_retention_and_expiration_policies',
|
|
|
|
};
|
2020-01-21 18:07:31 +00:00
|
|
|
|
2020-02-04 12:09:00 +00:00
|
|
|
const findForm = () => wrapper.find({ ref: 'form-element' });
|
|
|
|
const findFields = () => wrapper.find(expirationPolicyFields);
|
|
|
|
const findCancelButton = () => wrapper.find({ ref: 'cancel-button' });
|
|
|
|
const findSaveButton = () => wrapper.find({ ref: 'save-button' });
|
2019-12-19 12:07:35 +00:00
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
const mountComponent = ({
|
|
|
|
props = defaultProps,
|
|
|
|
data,
|
|
|
|
config,
|
|
|
|
provide = defaultProvidedValues,
|
|
|
|
mocks,
|
|
|
|
} = {}) => {
|
2020-02-03 09:08:42 +00:00
|
|
|
wrapper = shallowMount(component, {
|
2020-02-04 12:09:00 +00:00
|
|
|
stubs: {
|
|
|
|
GlCard,
|
|
|
|
GlLoadingIcon,
|
|
|
|
},
|
2020-09-30 12:09:53 +00:00
|
|
|
propsData: { ...props },
|
|
|
|
provide,
|
2020-07-14 12:09:14 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
2020-01-21 18:07:31 +00:00
|
|
|
mocks: {
|
|
|
|
$toast: {
|
|
|
|
show: jest.fn(),
|
|
|
|
},
|
2020-09-30 12:09:53 +00:00
|
|
|
...mocks,
|
2019-12-19 12:07:35 +00:00
|
|
|
},
|
2020-09-30 12:09:53 +00:00
|
|
|
...config,
|
2019-12-19 12:07:35 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
const mountComponentWithApollo = ({ provide = defaultProvidedValues, resolver } = {}) => {
|
|
|
|
localVue.use(VueApollo);
|
|
|
|
|
|
|
|
const requestHandlers = [
|
|
|
|
[updateContainerExpirationPolicyMutation, resolver],
|
|
|
|
[expirationPolicyQuery, jest.fn().mockResolvedValue(expirationPolicyPayload())],
|
|
|
|
];
|
|
|
|
|
|
|
|
fakeApollo = createMockApollo(requestHandlers);
|
|
|
|
|
|
|
|
fakeApollo.defaultClient.cache.writeQuery({
|
|
|
|
query: expirationPolicyQuery,
|
|
|
|
variables: {
|
|
|
|
projectPath: provide.projectPath,
|
|
|
|
},
|
|
|
|
...expirationPolicyPayload(),
|
|
|
|
});
|
|
|
|
|
|
|
|
mountComponent({
|
|
|
|
provide,
|
|
|
|
config: {
|
|
|
|
localVue,
|
|
|
|
apolloProvider: fakeApollo,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return requestHandlers.map(resolvers => resolvers[1]);
|
|
|
|
};
|
|
|
|
|
2019-12-19 12:07:35 +00:00
|
|
|
beforeEach(() => {
|
2020-01-29 12:09:08 +00:00
|
|
|
jest.spyOn(Tracking, 'event');
|
2019-12-19 12:07:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
describe('data binding', () => {
|
|
|
|
it('v-model change update the settings property', () => {
|
|
|
|
mountComponent();
|
|
|
|
findFields().vm.$emit('input', { newValue: 'foo' });
|
2020-09-30 12:09:53 +00:00
|
|
|
expect(wrapper.emitted('input')).toEqual([['foo']]);
|
2020-07-14 12:09:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('v-model change update the api error property', () => {
|
|
|
|
const apiErrors = { baz: 'bar' };
|
2020-09-30 12:09:53 +00:00
|
|
|
mountComponent({ data: { apiErrors } });
|
2020-07-14 12:09:14 +00:00
|
|
|
expect(findFields().props('apiErrors')).toEqual(apiErrors);
|
|
|
|
findFields().vm.$emit('input', { newValue: 'foo', modified: 'baz' });
|
|
|
|
expect(findFields().props('apiErrors')).toEqual({});
|
|
|
|
});
|
2020-10-05 09:08:17 +00:00
|
|
|
|
|
|
|
it('shows the default option when none are selected', () => {
|
|
|
|
mountComponent({ props: { value: {} } });
|
|
|
|
expect(findFields().props('value')).toEqual({
|
|
|
|
cadence: 'EVERY_DAY',
|
|
|
|
keepN: 'TEN_TAGS',
|
|
|
|
olderThan: 'NINETY_DAYS',
|
|
|
|
});
|
|
|
|
});
|
2020-07-14 12:09:14 +00:00
|
|
|
});
|
|
|
|
|
2020-02-03 09:08:42 +00:00
|
|
|
describe('form', () => {
|
|
|
|
describe('form reset event', () => {
|
2020-02-04 12:09:00 +00:00
|
|
|
beforeEach(() => {
|
2020-09-30 12:09:53 +00:00
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
findForm().trigger('reset');
|
2020-02-04 12:09:00 +00:00
|
|
|
});
|
2020-01-29 12:09:08 +00:00
|
|
|
it('calls the appropriate function', () => {
|
2020-09-30 12:09:53 +00:00
|
|
|
expect(wrapper.emitted('reset')).toEqual([[]]);
|
2020-01-29 12:09:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('tracks the reset event', () => {
|
|
|
|
expect(Tracking.event).toHaveBeenCalledWith(undefined, 'reset_form', trackingPayload);
|
|
|
|
});
|
2019-12-19 12:07:35 +00:00
|
|
|
});
|
|
|
|
|
2020-01-21 18:07:31 +00:00
|
|
|
describe('form submit event ', () => {
|
2020-02-04 12:09:00 +00:00
|
|
|
it('save has type submit', () => {
|
|
|
|
mountComponent();
|
2020-09-30 12:09:53 +00:00
|
|
|
|
2020-02-04 12:09:00 +00:00
|
|
|
expect(findSaveButton().attributes('type')).toBe('submit');
|
|
|
|
});
|
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
it('dispatches the correct apollo mutation', async () => {
|
|
|
|
const [expirationPolicyMutationResolver] = mountComponentWithApollo({
|
|
|
|
resolver: jest.fn().mockResolvedValue(expirationPolicyMutationPayload()),
|
|
|
|
});
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await expirationPolicyMutationResolver();
|
|
|
|
expect(expirationPolicyMutationResolver).toHaveBeenCalled();
|
2020-01-21 18:07:31 +00:00
|
|
|
});
|
|
|
|
|
2020-01-29 12:09:08 +00:00
|
|
|
it('tracks the submit event', () => {
|
2020-09-30 12:09:53 +00:00
|
|
|
mountComponentWithApollo({
|
|
|
|
resolver: jest.fn().mockResolvedValue(expirationPolicyMutationPayload()),
|
|
|
|
});
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
|
2020-01-29 12:09:08 +00:00
|
|
|
expect(Tracking.event).toHaveBeenCalledWith(undefined, 'submit_form', trackingPayload);
|
|
|
|
});
|
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
it('show a success toast when submit succeed', async () => {
|
2020-09-30 12:09:53 +00:00
|
|
|
const handlers = mountComponentWithApollo({
|
|
|
|
resolver: jest.fn().mockResolvedValue(expirationPolicyMutationPayload()),
|
|
|
|
});
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await Promise.all(handlers);
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(UPDATE_SETTINGS_SUCCESS_MESSAGE, {
|
|
|
|
type: 'success',
|
2020-01-21 18:07:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
describe('when submit fails', () => {
|
2020-09-30 12:09:53 +00:00
|
|
|
describe('user recoverable errors', () => {
|
|
|
|
it('when there is an error is shown in a toast', async () => {
|
|
|
|
const handlers = mountComponentWithApollo({
|
|
|
|
resolver: jest
|
|
|
|
.fn()
|
|
|
|
.mockResolvedValue(expirationPolicyMutationPayload({ errors: ['foo'] })),
|
|
|
|
});
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await Promise.all(handlers);
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
|
|
|
|
expect(wrapper.vm.$toast.show).toHaveBeenCalledWith('foo', {
|
|
|
|
type: 'error',
|
|
|
|
});
|
2020-01-21 18:07:31 +00:00
|
|
|
});
|
|
|
|
});
|
2020-09-30 12:09:53 +00:00
|
|
|
describe('global errors', () => {
|
|
|
|
it('shows an error', async () => {
|
|
|
|
const handlers = mountComponentWithApollo({
|
|
|
|
resolver: jest.fn().mockRejectedValue(expirationPolicyMutationPayload()),
|
|
|
|
});
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await Promise.all(handlers);
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
await wrapper.vm.$nextTick();
|
2020-07-14 12:09:14 +00:00
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(UPDATE_SETTINGS_ERROR_MESSAGE, {
|
|
|
|
type: 'error',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('parses the error messages', async () => {
|
|
|
|
const mutate = jest.fn().mockRejectedValue({
|
|
|
|
graphQLErrors: [
|
|
|
|
{
|
|
|
|
extensions: {
|
|
|
|
problems: [{ path: ['name'], message: 'baz' }],
|
|
|
|
},
|
2020-07-14 12:09:14 +00:00
|
|
|
},
|
2020-09-30 12:09:53 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
mountComponent({ mocks: { $apollo: { mutate } } });
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await waitForPromises();
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
|
|
|
|
expect(findFields().props('apiErrors')).toEqual({ name: 'baz' });
|
2020-07-14 12:09:14 +00:00
|
|
|
});
|
|
|
|
});
|
2020-01-21 18:07:31 +00:00
|
|
|
});
|
2019-12-19 12:07:35 +00:00
|
|
|
});
|
|
|
|
});
|
2020-02-04 12:09:00 +00:00
|
|
|
|
|
|
|
describe('form actions', () => {
|
|
|
|
describe('cancel button', () => {
|
2020-09-30 12:09:53 +00:00
|
|
|
it('has type reset', () => {
|
2020-07-14 12:09:14 +00:00
|
|
|
mountComponent();
|
2020-02-04 12:09:00 +00:00
|
|
|
|
|
|
|
expect(findCancelButton().attributes('type')).toBe('reset');
|
|
|
|
});
|
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
it.each`
|
|
|
|
isLoading | isEdited | mutationLoading | isDisabled
|
|
|
|
${true} | ${true} | ${true} | ${true}
|
|
|
|
${false} | ${true} | ${true} | ${true}
|
|
|
|
${false} | ${false} | ${true} | ${true}
|
|
|
|
${true} | ${false} | ${false} | ${true}
|
|
|
|
${false} | ${false} | ${false} | ${true}
|
|
|
|
${false} | ${true} | ${false} | ${false}
|
|
|
|
`(
|
|
|
|
'when isLoading is $isLoading and isEdited is $isEdited and mutationLoading is $mutationLoading is $isDisabled that the is disabled',
|
|
|
|
({ isEdited, isLoading, mutationLoading, isDisabled }) => {
|
|
|
|
mountComponent({
|
|
|
|
props: { ...defaultProps, isEdited, isLoading },
|
|
|
|
data: { mutationLoading },
|
|
|
|
});
|
2020-02-04 12:09:00 +00:00
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
const expectation = isDisabled ? 'true' : undefined;
|
|
|
|
expect(findCancelButton().attributes('disabled')).toBe(expectation);
|
|
|
|
},
|
|
|
|
);
|
2020-02-04 12:09:00 +00:00
|
|
|
});
|
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
describe('submit button', () => {
|
|
|
|
it('has type submit', () => {
|
2020-07-14 12:09:14 +00:00
|
|
|
mountComponent();
|
2020-02-04 12:09:00 +00:00
|
|
|
|
2020-09-30 12:09:53 +00:00
|
|
|
expect(findSaveButton().attributes('type')).toBe('submit');
|
2020-02-04 12:09:00 +00:00
|
|
|
});
|
2020-09-30 12:09:53 +00:00
|
|
|
it.each`
|
|
|
|
isLoading | fieldsAreValid | mutationLoading | isDisabled
|
|
|
|
${true} | ${true} | ${true} | ${true}
|
|
|
|
${false} | ${true} | ${true} | ${true}
|
|
|
|
${false} | ${false} | ${true} | ${true}
|
|
|
|
${true} | ${false} | ${false} | ${true}
|
|
|
|
${false} | ${false} | ${false} | ${true}
|
|
|
|
${false} | ${true} | ${false} | ${false}
|
|
|
|
`(
|
|
|
|
'when isLoading is $isLoading and fieldsAreValid is $fieldsAreValid and mutationLoading is $mutationLoading is $isDisabled that the is disabled',
|
|
|
|
({ fieldsAreValid, isLoading, mutationLoading, isDisabled }) => {
|
|
|
|
mountComponent({
|
|
|
|
props: { ...defaultProps, isLoading },
|
|
|
|
data: { mutationLoading, fieldsAreValid },
|
|
|
|
});
|
|
|
|
|
|
|
|
const expectation = isDisabled ? 'true' : undefined;
|
|
|
|
expect(findSaveButton().attributes('disabled')).toBe(expectation);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
isLoading | mutationLoading | showLoading
|
|
|
|
${true} | ${true} | ${true}
|
|
|
|
${true} | ${false} | ${true}
|
|
|
|
${false} | ${true} | ${true}
|
|
|
|
${false} | ${false} | ${false}
|
|
|
|
`(
|
|
|
|
'when isLoading is $isLoading and mutationLoading is $mutationLoading is $showLoading that the loading icon is shown',
|
|
|
|
({ isLoading, mutationLoading, showLoading }) => {
|
|
|
|
mountComponent({
|
|
|
|
props: { ...defaultProps, isLoading },
|
|
|
|
data: { mutationLoading },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(findSaveButton().props('loading')).toBe(showLoading);
|
|
|
|
},
|
|
|
|
);
|
2020-02-04 12:09:00 +00:00
|
|
|
});
|
|
|
|
});
|
2019-12-19 12:07:35 +00:00
|
|
|
});
|