Commit da306cdb authored by Nathan Friend's avatar Nathan Friend

Convert spec/javascripts/environments/*rollback* tests to Jest

This commit converts two Jasmine tests into Jest tests.
parent 22f3142b
...@@ -61,7 +61,7 @@ describe('Confirm Rollback Modal Component', () => { ...@@ -61,7 +61,7 @@ describe('Confirm Rollback Modal Component', () => {
environment, environment,
}, },
}); });
const eventHubSpy = spyOn(eventHub, '$emit'); const eventHubSpy = jest.spyOn(eventHub, '$emit');
const modal = component.find(GlModal); const modal = component.find(GlModal);
modal.vm.$emit('ok'); modal.vm.$emit('ok');
......
import Vue from 'vue'; import { shallowMount, mount } from '@vue/test-utils';
import { shallowMount } from '@vue/test-utils';
import { GlButton } from '@gitlab/ui'; import { GlButton } from '@gitlab/ui';
import eventHub from '~/environments/event_hub'; import eventHub from '~/environments/event_hub';
import rollbackComp from '~/environments/components/environment_rollback.vue'; import RollbackComponent from '~/environments/components/environment_rollback.vue';
describe('Rollback Component', () => { describe('Rollback Component', () => {
const retryUrl = 'https://gitlab.com/retry'; const retryUrl = 'https://gitlab.com/retry';
let RollbackComponent;
beforeEach(() => {
RollbackComponent = Vue.extend(rollbackComp);
});
it('Should render Re-deploy label when isLastDeployment is true', () => { it('Should render Re-deploy label when isLastDeployment is true', () => {
const component = new RollbackComponent({ const wrapper = mount(RollbackComponent, {
el: document.querySelector('.test-dom-element'),
propsData: { propsData: {
retryUrl, retryUrl,
isLastDeployment: true, isLastDeployment: true,
environment: {}, environment: {},
}, },
}).$mount(); });
expect(component.$el).toHaveSpriteIcon('repeat'); expect(wrapper.element).toHaveSpriteIcon('repeat');
}); });
it('Should render Rollback label when isLastDeployment is false', () => { it('Should render Rollback label when isLastDeployment is false', () => {
const component = new RollbackComponent({ const wrapper = mount(RollbackComponent, {
el: document.querySelector('.test-dom-element'),
propsData: { propsData: {
retryUrl, retryUrl,
isLastDeployment: false, isLastDeployment: false,
environment: {}, environment: {},
}, },
}).$mount(); });
expect(component.$el).toHaveSpriteIcon('redo'); expect(wrapper.element).toHaveSpriteIcon('redo');
}); });
it('should emit a "rollback" event on button click', () => { it('should emit a "rollback" event on button click', () => {
const eventHubSpy = spyOn(eventHub, '$emit'); const eventHubSpy = jest.spyOn(eventHub, '$emit');
const component = shallowMount(RollbackComponent, { const wrapper = shallowMount(RollbackComponent, {
propsData: { propsData: {
retryUrl, retryUrl,
environment: { environment: {
...@@ -48,7 +40,7 @@ describe('Rollback Component', () => { ...@@ -48,7 +40,7 @@ describe('Rollback Component', () => {
}, },
}, },
}); });
const button = component.find(GlButton); const button = wrapper.find(GlButton);
button.vm.$emit('click'); button.vm.$emit('click');
......
export default {
toHaveSpriteIcon: (element, iconName) => {
if (!iconName) {
throw new Error('toHaveSpriteIcon is missing iconName argument!');
}
if (!(element instanceof HTMLElement)) {
throw new Error(`${element} is not a DOM element!`);
}
const iconReferences = [].slice.apply(element.querySelectorAll('svg use'));
const matchingIcon = iconReferences.find(reference =>
reference.getAttribute('xlink:href').endsWith(`#${iconName}`),
);
const pass = Boolean(matchingIcon);
let message;
if (pass) {
message = `${element.outerHTML} contains the sprite icon "${iconName}"!`;
} else {
message = `${element.outerHTML} does not contain the sprite icon "${iconName}"!`;
const existingIcons = iconReferences.map(reference => {
const iconUrl = reference.getAttribute('xlink:href');
return `"${iconUrl.replace(/^.+#/, '')}"`;
});
if (existingIcons.length > 0) {
message += ` (only found ${existingIcons.join(',')})`;
}
}
return {
pass,
message: () => message,
};
},
};
...@@ -6,6 +6,7 @@ import { config as testUtilsConfig } from '@vue/test-utils'; ...@@ -6,6 +6,7 @@ import { config as testUtilsConfig } from '@vue/test-utils';
import { initializeTestTimeout } from './helpers/timeout'; import { initializeTestTimeout } from './helpers/timeout';
import { loadHTMLFixture, setHTMLFixture } from './helpers/fixtures'; import { loadHTMLFixture, setHTMLFixture } from './helpers/fixtures';
import { setupManualMocks } from './mocks/mocks_helper'; import { setupManualMocks } from './mocks/mocks_helper';
import customMatchers from './matchers';
// Expose jQuery so specs using jQuery plugins can be imported nicely. // Expose jQuery so specs using jQuery plugins can be imported nicely.
// Here is an issue to explore better alternatives: // Here is an issue to explore better alternatives:
...@@ -67,6 +68,8 @@ Object.entries(jqueryMatchers).forEach(([matcherName, matcherFactory]) => { ...@@ -67,6 +68,8 @@ Object.entries(jqueryMatchers).forEach(([matcherName, matcherFactory]) => {
}); });
}); });
expect.extend(customMatchers);
// Tech debt issue TBD // Tech debt issue TBD
testUtilsConfig.logModifiedComponents = false; testUtilsConfig.logModifiedComponents = false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment