2019-02-15 15:39:39 +05:30
|
|
|
import Vue from 'vue';
|
|
|
|
import SuggestionsComponent from '~/vue_shared/components/markdown/suggestions.vue';
|
|
|
|
|
|
|
|
const MOCK_DATA = {
|
2019-07-07 11:18:12 +05:30
|
|
|
suggestions: [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
appliable: true,
|
|
|
|
applied: false,
|
|
|
|
current_user: {
|
|
|
|
can_apply: true,
|
|
|
|
},
|
|
|
|
diff_lines: [
|
|
|
|
{
|
|
|
|
can_receive_suggestion: false,
|
|
|
|
line_code: null,
|
|
|
|
meta_data: null,
|
|
|
|
new_line: null,
|
|
|
|
old_line: 5,
|
|
|
|
rich_text: '-test',
|
|
|
|
text: '-test',
|
|
|
|
type: 'old',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
can_receive_suggestion: true,
|
|
|
|
line_code: null,
|
|
|
|
meta_data: null,
|
|
|
|
new_line: 5,
|
|
|
|
old_line: null,
|
|
|
|
rich_text: '+new test',
|
|
|
|
text: '+new test',
|
|
|
|
type: 'new',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2019-02-15 15:39:39 +05:30
|
|
|
noteHtml: `
|
2019-07-07 11:18:12 +05:30
|
|
|
<div class="suggestion">
|
|
|
|
<div class="line">-oldtest</div>
|
2020-05-24 23:13:21 +05:30
|
|
|
</div>
|
2019-02-15 15:39:39 +05:30
|
|
|
<div class="suggestion">
|
2019-07-07 11:18:12 +05:30
|
|
|
<div class="line">+newtest</div>
|
2020-05-24 23:13:21 +05:30
|
|
|
</div>
|
2019-02-15 15:39:39 +05:30
|
|
|
`,
|
|
|
|
isApplied: false,
|
|
|
|
helpPagePath: 'path_to_docs',
|
2021-03-08 18:12:59 +05:30
|
|
|
defaultCommitMessage: 'Apply suggestion',
|
2019-02-15 15:39:39 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
describe('Suggestion component', () => {
|
|
|
|
let vm;
|
|
|
|
let diffTable;
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
beforeEach((done) => {
|
2019-02-15 15:39:39 +05:30
|
|
|
const Component = Vue.extend(SuggestionsComponent);
|
|
|
|
|
|
|
|
vm = new Component({
|
|
|
|
propsData: MOCK_DATA,
|
|
|
|
}).$mount();
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
diffTable = vm.generateDiff(0).$mount().$el;
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(vm, 'renderSuggestions').mockImplementation(() => {});
|
2019-02-15 15:39:39 +05:30
|
|
|
vm.renderSuggestions();
|
|
|
|
Vue.nextTick(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('mounted', () => {
|
|
|
|
it('renders a flash container', () => {
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(vm.$el.querySelector('.js-suggestions-flash')).not.toBeNull();
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a container for suggestions', () => {
|
|
|
|
expect(vm.$refs.container).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders suggestions', () => {
|
|
|
|
expect(vm.renderSuggestions).toHaveBeenCalled();
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(vm.$el.innerHTML.includes('oldtest')).toBe(true);
|
|
|
|
expect(vm.$el.innerHTML.includes('newtest')).toBe(true);
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('generateDiff', () => {
|
|
|
|
it('generates a diff table', () => {
|
|
|
|
expect(diffTable.querySelector('.md-suggestion-diff')).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('generates a diff table that contains contents the suggested lines', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
MOCK_DATA.suggestions[0].diff_lines.forEach((line) => {
|
2019-07-07 11:18:12 +05:30
|
|
|
const text = line.text.substring(1);
|
|
|
|
|
|
|
|
expect(diffTable.innerHTML.includes(text)).toBe(true);
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('generates a diff table with the correct line number for each suggested line', () => {
|
2019-07-07 11:18:12 +05:30
|
|
|
const lines = diffTable.querySelectorAll('.old_line');
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(parseInt([...lines][0].innerHTML, 10)).toBe(5);
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|