debian-mirror-gitlab/spec/frontend/diffs/components/diff_stats_spec.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import { GlIcon } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { shallowMount } from '@vue/test-utils';
2021-09-04 01:27:46 +05:30
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
2019-03-02 22:35:43 +05:30
import DiffStats from '~/diffs/components/diff_stats.vue';
2021-09-04 01:27:46 +05:30
import mockDiffFile from '../mock_data/diff_file';
2019-03-02 22:35:43 +05:30
2020-10-24 23:57:45 +05:30
const TEST_ADDED_LINES = 100;
const TEST_REMOVED_LINES = 200;
const DIFF_FILES_COUNT = '300';
const DIFF_FILES_COUNT_TRUNCATED = '300+';
2019-03-02 22:35:43 +05:30
describe('diff_stats', () => {
2020-10-24 23:57:45 +05:30
let wrapper;
const createComponent = (props = {}) => {
2021-09-04 01:27:46 +05:30
wrapper = extendedWrapper(
shallowMount(DiffStats, {
propsData: {
addedLines: TEST_ADDED_LINES,
removedLines: TEST_REMOVED_LINES,
...props,
},
}),
);
2020-10-24 23:57:45 +05:30
};
2019-03-02 22:35:43 +05:30
2020-10-24 23:57:45 +05:30
describe('diff stats group', () => {
const findDiffStatsGroup = () => wrapper.findAll('.diff-stats-group');
2019-03-02 22:35:43 +05:30
2020-10-24 23:57:45 +05:30
it('is not rendered if diffFilesCountText is empty', () => {
createComponent();
expect(findDiffStatsGroup().length).toBe(2);
2020-03-13 15:44:24 +05:30
});
2020-10-24 23:57:45 +05:30
it('is not rendered if diffFilesCountText is not a number', () => {
createComponent({
diffFilesCountText: null,
});
2020-03-13 15:44:24 +05:30
2020-10-24 23:57:45 +05:30
expect(findDiffStatsGroup().length).toBe(2);
2019-03-02 22:35:43 +05:30
});
2020-10-24 23:57:45 +05:30
});
2019-03-02 22:35:43 +05:30
2021-09-04 01:27:46 +05:30
describe('bytes changes', () => {
let file;
const getBytesContainer = () => wrapper.find('.diff-stats > div:first-child');
beforeEach(() => {
file = {
...mockDiffFile,
viewer: {
...mockDiffFile.viewer,
name: 'not_diffable',
},
};
createComponent({ diffFile: file });
});
it("renders the bytes changes instead of line changes when the file isn't diffable", () => {
const content = getBytesContainer();
expect(content.classes('gl-text-green-600')).toBe(true);
expect(content.text()).toBe('+1.00 KiB (+100%)');
});
});
2020-10-24 23:57:45 +05:30
describe('line changes', () => {
2021-09-04 01:27:46 +05:30
const findFileLine = (name) => wrapper.findByTestId(name);
beforeEach(() => {
createComponent();
});
2020-10-24 23:57:45 +05:30
it('shows the amount of lines added', () => {
2021-09-04 01:27:46 +05:30
expect(findFileLine('js-file-addition-line').text()).toBe(TEST_ADDED_LINES.toString());
2020-10-24 23:57:45 +05:30
});
it('shows the amount of lines removed', () => {
2021-09-04 01:27:46 +05:30
expect(findFileLine('js-file-deletion-line').text()).toBe(TEST_REMOVED_LINES.toString());
2020-10-24 23:57:45 +05:30
});
});
describe('files changes', () => {
2021-03-08 18:12:59 +05:30
const findIcon = (name) =>
2020-03-13 15:44:24 +05:30
wrapper
2020-11-24 15:15:51 +05:30
.findAll(GlIcon)
2021-03-08 18:12:59 +05:30
.filter((c) => c.attributes('name') === name)
2020-03-13 15:44:24 +05:30
.at(0).element.parentNode;
2020-10-24 23:57:45 +05:30
it('shows amount of file changed with plural "files" when 0 files has changed', () => {
const oneFileChanged = '0';
createComponent({
diffFilesCountText: oneFileChanged,
});
expect(findIcon('doc-code').textContent.trim()).toBe(`${oneFileChanged} files`);
});
it('shows amount of file changed with singular "file" when 1 file is changed', () => {
const oneFileChanged = '1';
createComponent({
diffFilesCountText: oneFileChanged,
});
expect(findIcon('doc-code').textContent.trim()).toBe(`${oneFileChanged} file`);
});
it('shows amount of files change with plural "files" when multiple files are changed', () => {
createComponent({
diffFilesCountText: DIFF_FILES_COUNT,
});
expect(findIcon('doc-code').textContent.trim()).toContain(`${DIFF_FILES_COUNT} files`);
});
it('shows amount of files change with plural "files" when files changed is truncated', () => {
createComponent({
diffFilesCountText: DIFF_FILES_COUNT_TRUNCATED,
});
expect(findIcon('doc-code').textContent.trim()).toContain(
`${DIFF_FILES_COUNT_TRUNCATED} files`,
);
});
2019-03-02 22:35:43 +05:30
});
});