Commit ba0724ba authored by Simon Knox's avatar Simon Knox

Merge branch 'peterhegman/switch-to-vuex-for-group-members-app' into 'master'

Move state from root instance to Vuex

See merge request gitlab-org/gitlab!42135
parents c5307cca 9eb1bf9c
<script> <script>
export default { export default {
name: 'GroupMembersApp', name: 'GroupMembersApp',
props: {
groupId: {
type: Number,
required: true,
},
currentUserId: {
type: Number,
required: false,
default: null,
},
members: {
type: Array,
required: true,
},
},
}; };
</script> </script>
......
import Vue from 'vue'; import Vue from 'vue';
import Vuex from 'vuex';
import App from './components/app.vue'; import App from './components/app.vue';
import membersModule from '~/vuex_shared/modules/members';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils'; import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
export default el => { export default el => {
...@@ -7,26 +9,22 @@ export default el => { ...@@ -7,26 +9,22 @@ export default el => {
return () => {}; return () => {};
} }
Vue.use(Vuex);
const { members, groupId } = el.dataset;
const store = new Vuex.Store({
...membersModule({
members: convertObjectPropsToCamelCase(JSON.parse(members), { deep: true }),
sourceId: parseInt(groupId, 10),
currentUserId: gon.current_user_id || null,
}),
});
return new Vue({ return new Vue({
el, el,
components: { App }, components: { App },
data() { store,
const { members, groupId, currentUserId } = this.$options.el.dataset; render: createElement => createElement('app'),
return {
members: convertObjectPropsToCamelCase(JSON.parse(members), { deep: true }),
groupId: parseInt(groupId, 10),
...(currentUserId ? { currentUserId: parseInt(currentUserId, 10) } : {}),
};
},
render(createElement) {
return createElement('app', {
props: {
members: this.members,
groupId: this.groupId,
currentUserId: this.currentUserId,
},
});
},
}); });
}; };
import createState from './state';
export default initialState => ({
namespaced: true,
state: createState(initialState),
});
export default ({ members, sourceId, currentUserId }) => ({
members,
sourceId,
currentUserId,
});
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
- show_access_requests = can_manage_members && @requesters.exists? - show_access_requests = can_manage_members && @requesters.exists?
- invited_active = params[:search_invited].present? || params[:invited_members_page].present? - invited_active = params[:search_invited].present? || params[:invited_members_page].present?
- vue_members_list_enabled = Feature.enabled?(:vue_group_members_list, @group) - vue_members_list_enabled = Feature.enabled?(:vue_group_members_list, @group)
- data_attributes = { group_id: @group.id, current_user_id: current_user&.id } - data_attributes = { group_id: @group.id }
- form_item_label_css_class = 'label-bold gl-mr-2 gl-mb-0 gl-py-2 align-self-md-center' - form_item_label_css_class = 'label-bold gl-mr-2 gl-mb-0 gl-py-2 align-self-md-center'
......
...@@ -5,21 +5,21 @@ import { membersJsonString, membersParsed } from './mock_data'; ...@@ -5,21 +5,21 @@ import { membersJsonString, membersParsed } from './mock_data';
describe('initGroupMembersApp', () => { describe('initGroupMembersApp', () => {
let el; let el;
let vm;
let wrapper; let wrapper;
const setup = () => { const setup = () => {
const vm = initGroupMembersApp(el); vm = initGroupMembersApp(el);
wrapper = createWrapper(vm); wrapper = createWrapper(vm);
}; };
const getGroupMembersApp = () => wrapper.find(GroupMembersApp);
beforeEach(() => { beforeEach(() => {
el = document.createElement('div'); el = document.createElement('div');
el.setAttribute('data-members', membersJsonString); el.setAttribute('data-members', membersJsonString);
el.setAttribute('data-current-user-id', '123');
el.setAttribute('data-group-id', '234'); el.setAttribute('data-group-id', '234');
window.gon = { current_user_id: 123 };
document.body.appendChild(el); document.body.appendChild(el);
}); });
...@@ -31,28 +31,36 @@ describe('initGroupMembersApp', () => { ...@@ -31,28 +31,36 @@ describe('initGroupMembersApp', () => {
wrapper = null; wrapper = null;
}); });
it('parses and passes `currentUserId` prop to `GroupMembersApp`', () => { it('renders `GroupMembersApp`', () => {
setup(); setup();
expect(getGroupMembersApp().props('currentUserId')).toBe(123); expect(wrapper.find(GroupMembersApp).exists()).toBe(true);
}); });
it('does not pass `currentUserId` prop if not provided by the data attribute (user is not logged in)', () => { it('sets `currentUserId` in Vuex store', () => {
el.removeAttribute('data-current-user-id');
setup(); setup();
expect(getGroupMembersApp().props('currentUserId')).toBeNull(); expect(vm.$store.state.currentUserId).toBe(123);
});
describe('when `gon.current_user_id` is not set (user is not logged in)', () => {
it('sets `currentUserId` as `null` in Vuex store', () => {
window.gon = {};
setup();
expect(vm.$store.state.currentUserId).toBeNull();
});
}); });
it('parses and passes `groupId` prop to `GroupMembersApp`', () => { it('parses and sets `data-group-id` as `sourceId` in Vuex store', () => {
setup(); setup();
expect(getGroupMembersApp().props('groupId')).toBe(234); expect(vm.$store.state.sourceId).toBe(234);
}); });
it('parses and passes `members` prop to `GroupMembersApp`', () => { it('parses and sets `members` in Vuex store', () => {
setup(); setup();
expect(getGroupMembersApp().props('members')).toEqual(membersParsed); expect(vm.$store.state.members).toEqual(membersParsed);
}); });
}); });
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