Commit 84626f0d authored by Phil Hughes's avatar Phil Hughes

Merge branch '49161-disable-toggle-comments' into 'master'

Resolve "MR refactor: Disable toggle comments button when diff has no discussions"

Closes #49161

See merge request gitlab-org/gitlab-ce!20667
parents 2ff8b931 32de3753
...@@ -50,7 +50,7 @@ export default { ...@@ -50,7 +50,7 @@ export default {
}; };
}, },
computed: { computed: {
...mapGetters('diffs', ['diffHasExpandedDiscussions']), ...mapGetters('diffs', ['diffHasExpandedDiscussions', 'diffHasDiscussions']),
hasExpandedDiscussions() { hasExpandedDiscussions() {
return this.diffHasExpandedDiscussions(this.diffFile); return this.diffHasExpandedDiscussions(this.diffFile);
}, },
...@@ -221,6 +221,7 @@ export default { ...@@ -221,6 +221,7 @@ export default {
v-if="diffFile.blob && diffFile.blob.readableText" v-if="diffFile.blob && diffFile.blob.readableText"
> >
<button <button
:disabled="!diffHasDiscussions(diffFile)"
:class="{ active: hasExpandedDiscussions }" :class="{ active: hasExpandedDiscussions }"
:title="s__('MergeRequests|Toggle comments for this file')" :title="s__('MergeRequests|Toggle comments for this file')"
class="js-btn-vue-toggle-comments btn" class="js-btn-vue-toggle-comments btn"
......
...@@ -47,6 +47,14 @@ export const diffHasExpandedDiscussions = (state, getters) => diff => { ...@@ -47,6 +47,14 @@ export const diffHasExpandedDiscussions = (state, getters) => diff => {
); );
}; };
/**
* Checks if the diff has any discussion
* @param {Boolean} diff
* @returns {Boolean}
*/
export const diffHasDiscussions = (state, getters) => diff =>
getters.getDiffFileDiscussions(diff).length > 0;
/** /**
* Returns an array with the discussions of the given diff * Returns an array with the discussions of the given diff
* @param {Object} diff * @param {Object} diff
......
---
title: Disables toggle comments button if diff has no discussions
merge_request:
author:
type: other
...@@ -11,7 +11,9 @@ const discussionFixture = 'merge_requests/diff_discussion.json'; ...@@ -11,7 +11,9 @@ const discussionFixture = 'merge_requests/diff_discussion.json';
describe('diff_file_header', () => { describe('diff_file_header', () => {
let vm; let vm;
let props; let props;
const diffDiscussionMock = getJSONFixture(discussionFixture)[0];
const Component = Vue.extend(DiffFileHeader); const Component = Vue.extend(DiffFileHeader);
const store = new Vuex.Store({ const store = new Vuex.Store({
modules: { modules: {
diffs: diffsModule, diffs: diffsModule,
...@@ -20,7 +22,6 @@ describe('diff_file_header', () => { ...@@ -20,7 +22,6 @@ describe('diff_file_header', () => {
}); });
beforeEach(() => { beforeEach(() => {
const diffDiscussionMock = getJSONFixture(discussionFixture)[0];
const diffFile = convertObjectPropsToCamelCase(diffDiscussionMock.diff_file, { deep: true }); const diffFile = convertObjectPropsToCamelCase(diffDiscussionMock.diff_file, { deep: true });
props = { props = {
diffFile, diffFile,
...@@ -409,7 +410,7 @@ describe('diff_file_header', () => { ...@@ -409,7 +410,7 @@ describe('diff_file_header', () => {
}); });
describe('handles toggle discussions', () => { describe('handles toggle discussions', () => {
it('dispatches toggleFileDiscussions when user clicks on toggle discussions button', () => { it('renders a disabled button when diff has no discussions', () => {
const propsCopy = Object.assign({}, props); const propsCopy = Object.assign({}, props);
propsCopy.diffFile.submodule = false; propsCopy.diffFile.submodule = false;
propsCopy.diffFile.blob = { propsCopy.diffFile.blob = {
...@@ -428,11 +429,44 @@ describe('diff_file_header', () => { ...@@ -428,11 +429,44 @@ describe('diff_file_header', () => {
store, store,
}); });
spyOn(vm, 'toggleFileDiscussions'); expect(
vm.$el.querySelector('.js-btn-vue-toggle-comments').getAttribute('disabled'),
vm.$el.querySelector('.js-btn-vue-toggle-comments').click(); ).toEqual('disabled');
});
expect(vm.toggleFileDiscussions).toHaveBeenCalled();
describe('with discussions', () => {
it('dispatches toggleFileDiscussions when user clicks on toggle discussions button', () => {
const propsCopy = Object.assign({}, props);
propsCopy.diffFile.submodule = false;
propsCopy.diffFile.blob = {
id: '848ed9407c6730ff16edb3dd24485a0eea24292a',
path: 'lib/base.js',
name: 'base.js',
mode: '100644',
readableText: true,
icon: 'file-text-o',
};
propsCopy.addMergeRequestButtons = true;
propsCopy.diffFile.deletedFile = true;
const discussionGetter = () => [diffDiscussionMock];
notesModule.getters.discussions = discussionGetter;
vm = mountComponentWithStore(Component, {
props: propsCopy,
store: new Vuex.Store({
modules: {
diffs: diffsModule,
notes: notesModule,
},
}),
});
spyOn(vm, 'toggleFileDiscussions');
vm.$el.querySelector('.js-btn-vue-toggle-comments').click();
expect(vm.toggleFileDiscussions).toHaveBeenCalled();
});
}); });
}); });
}); });
......
...@@ -167,6 +167,24 @@ describe('Diffs Module Getters', () => { ...@@ -167,6 +167,24 @@ describe('Diffs Module Getters', () => {
}); });
}); });
describe('diffHasDiscussions', () => {
it('returns true when getDiffFileDiscussions returns discussions', () => {
expect(
getters.diffHasDiscussions(localState, {
getDiffFileDiscussions: () => [discussionMock],
})(diffFileMock),
).toEqual(true);
});
it('returns false when getDiffFileDiscussions returns no discussions', () => {
expect(
getters.diffHasDiscussions(localState, {
getDiffFileDiscussions: () => [],
})(diffFileMock),
).toEqual(false);
});
});
describe('getDiffFileDiscussions', () => { describe('getDiffFileDiscussions', () => {
it('returns an array with discussions when fileHash matches and the discussion belongs to a diff', () => { it('returns an array with discussions when fileHash matches and the discussion belongs to a diff', () => {
discussionMock.diff_file.file_hash = diffFileMock.fileHash; discussionMock.diff_file.file_hash = diffFileMock.fileHash;
......
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