Commit b73d9cd2 authored by Martin Wortschack's avatar Martin Wortschack

Add selectedProject and selectedGroup props

- This allows for passing a group/project to the dropdown via props
parent a1517120
...@@ -26,11 +26,16 @@ export default { ...@@ -26,11 +26,16 @@ export default {
required: false, required: false,
default: () => ({}), default: () => ({}),
}, },
defaultGroup: {
type: Object,
required: false,
default: () => ({}),
},
}, },
data() { data() {
return { return {
loading: true, loading: true,
selectedGroup: {}, selectedGroup: this.defaultGroup || {},
}; };
}, },
computed: { computed: {
......
...@@ -35,11 +35,16 @@ export default { ...@@ -35,11 +35,16 @@ export default {
required: false, required: false,
default: () => ({}), default: () => ({}),
}, },
defaultProjects: {
type: Array,
required: false,
default: () => [],
},
}, },
data() { data() {
return { return {
loading: true, loading: true,
selectedProjects: [], selectedProjects: this.defaultProjects || [],
}; };
}, },
computed: { computed: {
...@@ -74,7 +79,7 @@ export default { ...@@ -74,7 +79,7 @@ export default {
}, },
clicked: this.onClick.bind(this), clicked: this.onClick.bind(this),
data: this.fetchData.bind(this), data: this.fetchData.bind(this),
renderRow: group => this.rowTemplate(group), renderRow: project => this.rowTemplate(project),
text: project => project.name, text: project => project.name,
opened: e => e.target.querySelector('.dropdown-input-field').focus(), opened: e => e.target.querySelector('.dropdown-input-field').focus(),
}); });
...@@ -106,9 +111,13 @@ export default { ...@@ -106,9 +111,13 @@ export default {
}); });
}, },
rowTemplate(project) { rowTemplate(project) {
const selected = this.defaultProjects.length
? this.defaultProjects.find(p => p.id === project.id)
: false;
const isActiveClass = selected ? 'is-active' : '';
return ` return `
<li> <li>
<a href='#' class='dropdown-menu-link'> <a href='#' class='dropdown-menu-link ${isActiveClass}'>
${this.avatarTemplate(project)} ${this.avatarTemplate(project)}
<div class="align-middle">${_.escape(project.name)}</div> <div class="align-middle">${_.escape(project.name)}</div>
</a> </a>
......
...@@ -28,6 +28,15 @@ const groups = [ ...@@ -28,6 +28,15 @@ const groups = [
describe('GroupsDropdownFilter component', () => { describe('GroupsDropdownFilter component', () => {
let wrapper; let wrapper;
const createComponent = (props = {}) => {
wrapper = mount(GroupsDropdownFilter, {
sync: false,
propsData: {
...props,
},
});
};
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
}); });
...@@ -37,10 +46,9 @@ describe('GroupsDropdownFilter component', () => { ...@@ -37,10 +46,9 @@ describe('GroupsDropdownFilter component', () => {
Api.groups.mockImplementation((term, options, callback) => { Api.groups.mockImplementation((term, options, callback) => {
callback(groups); callback(groups);
}); });
wrapper = mount(GroupsDropdownFilter);
}); });
const findDropdown = () => wrapper.find('.dropdown'); const findDropdown = () => wrapper.find({ ref: 'groupsDropdown' });
const openDropdown = () => { const openDropdown = () => {
$(findDropdown().element) $(findDropdown().element)
.parent() .parent()
...@@ -48,13 +56,33 @@ describe('GroupsDropdownFilter component', () => { ...@@ -48,13 +56,33 @@ describe('GroupsDropdownFilter component', () => {
}; };
const findDropdownItems = () => findDropdown().findAll('a'); const findDropdownItems = () => findDropdown().findAll('a');
const findDropdownButton = () => findDropdown().find('button'); const findDropdownButton = () => findDropdown().find('button');
const findDropdownButtonAvatar = () => findDropdown().find('.gl-avatar');
it('should call glDropdown', () => { it('should call glDropdown', () => {
createComponent();
expect($.fn.glDropdown).toHaveBeenCalled(); expect($.fn.glDropdown).toHaveBeenCalled();
}); });
describe('when passed a defaultGroup as prop', () => {
beforeEach(() => {
createComponent({
defaultGroup: groups[0],
});
});
it("displays the defaultGroup's name", () => {
expect(findDropdownButton().text()).toContain(groups[0].name);
});
it("renders the defaultGroup's avatar", () => {
expect(findDropdownButtonAvatar().exists()).toBe(true);
});
});
describe('it renders the items correctly', () => { describe('it renders the items correctly', () => {
beforeEach(() => { beforeEach(() => {
createComponent();
openDropdown(); openDropdown();
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick();
...@@ -102,6 +130,8 @@ describe('GroupsDropdownFilter component', () => { ...@@ -102,6 +130,8 @@ describe('GroupsDropdownFilter component', () => {
describe('on group click', () => { describe('on group click', () => {
beforeEach(() => { beforeEach(() => {
createComponent();
openDropdown(); openDropdown();
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick();
......
...@@ -53,7 +53,7 @@ describe('ProjectsDropdownFilter component', () => { ...@@ -53,7 +53,7 @@ describe('ProjectsDropdownFilter component', () => {
}); });
}); });
const findDropdown = () => wrapper.find('.dropdown'); const findDropdown = () => wrapper.find({ ref: 'projectsDropdown' });
const openDropdown = () => { const openDropdown = () => {
$(findDropdown().element) $(findDropdown().element)
.parent() .parent()
...@@ -61,6 +61,7 @@ describe('ProjectsDropdownFilter component', () => { ...@@ -61,6 +61,7 @@ describe('ProjectsDropdownFilter component', () => {
}; };
const findDropdownItems = () => findDropdown().findAll('a'); const findDropdownItems = () => findDropdown().findAll('a');
const findDropdownButton = () => findDropdown().find('button'); const findDropdownButton = () => findDropdown().find('button');
const findDropdownButtonAvatar = () => findDropdown().find('.gl-avatar');
describe('queryParams are applied when fetching data', () => { describe('queryParams are applied when fetching data', () => {
beforeEach(() => { beforeEach(() => {
...@@ -87,6 +88,34 @@ describe('ProjectsDropdownFilter component', () => { ...@@ -87,6 +88,34 @@ describe('ProjectsDropdownFilter component', () => {
}); });
}); });
describe('when passed a an array of defaultProject as prop', () => {
beforeEach(() => {
createComponent({
defaultProjects: [projects[0]],
});
});
it("displays the defaultProject's name", () => {
expect(findDropdownButton().text()).toContain(projects[0].name);
});
it("renders the defaultProject's avatar", () => {
expect(findDropdownButtonAvatar().exists()).toBe(true);
});
it('marks the defaultProject as selected', () => {
openDropdown();
return wrapper.vm.$nextTick().then(() => {
expect(
findDropdownItems()
.at(0)
.classes('is-active'),
).toBe(true);
});
});
});
describe('when multiSelect is false', () => { describe('when multiSelect is false', () => {
beforeEach(() => { beforeEach(() => {
createComponent({ multiSelect: false }); createComponent({ multiSelect: 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