From 9c8dd06780da63118e517b51256b4cae982563d9 Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Wed, 27 Mar 2019 01:56:44 +0500 Subject: [PATCH] Fix fetching and searching tests for users --- src/api/__mocks__/users.js | 25 +++++++++---- test/views/users/index.test.js | 65 +++++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/src/api/__mocks__/users.js b/src/api/__mocks__/users.js index 73a93174..3cd9a86c 100644 --- a/src/api/__mocks__/users.js +++ b/src/api/__mocks__/users.js @@ -1,11 +1,22 @@ const users = [ - { deactivated: false, id: '2', nickname: 'allis', local: true, roles: { admin: true, moderator: false }, tags: [] }, - { deactivated: false, id: '10', nickname: 'bob', local: false, roles: { admin: false, moderator: true }, tags: ['sandbox'] }, - { deactivated: true, id: 'abc', nickname: 'john', local: true, roles: { admin: false, moderator: false }, tags: ['strip_media'] } + { active: true, deactivated: false, id: '2', nickname: 'allis', local: true, external: false, roles: { admin: true, moderator: false }, tags: [] }, + { active: true, deactivated: false, id: '10', nickname: 'bob', local: false, external: true, roles: { admin: false, moderator: true }, tags: ['sandbox'] }, + { active: false, deactivated: true, id: 'abc', nickname: 'john', local: true, external: false, roles: { admin: false, moderator: false }, tags: ['strip_media'] } ] -export async function fetchUsers(showLocalUsersOnly, authHost, token, page = 1) { - const filteredUsers = showLocalUsersOnly ? users.filter(user => user.local) : users +const filterUsers = (str) => { + const filters = str.split(',').filter(item => item.length > 0) + if (filters.length === 0) { + return users + } + return filters.reduce((filter, acc) => { + const filteredUsers = users.filter(user => user[filter]) + return [...acc, ...filteredUsers] + }, []) +} + +export async function fetchUsers(filters, authHost, token, page = 1) { + const filteredUsers = filterUsers(filters) return Promise.resolve({ data: { users: filteredUsers, count: filteredUsers.length, @@ -18,8 +29,8 @@ export async function toggleUserActivation(nickname, authHost, token) { return Promise.resolve({ data: { ...response, deactivated: !response.deactivated }}) } -export async function searchUsers(query, showLocalUsersOnly, authHost, token, page = 1) { - const filteredUsers = showLocalUsersOnly ? users.filter(user => user.local) : users +export async function searchUsers(query, filters, authHost, token, page = 1) { + const filteredUsers = filterUsers(filters) const response = filteredUsers.filter(user => user.nickname === query) return Promise.resolve({ data: { users: response, diff --git a/test/views/users/index.test.js b/test/views/users/index.test.js index b739abdb..a053c4d3 100644 --- a/test/views/users/index.test.js +++ b/test/views/users/index.test.js @@ -6,6 +6,9 @@ import storeConfig from './store.conf' import { cloneDeep } from 'lodash' config.mocks["$t"] = () => {} +config.mocks["$i18n"] = { + t: () => {} +} const localVue = createLocalVue() localVue.use(Vuex) @@ -23,7 +26,10 @@ describe('Search and filter users', () => { it('fetches initial list of users', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -34,7 +40,10 @@ describe('Search and filter users', () => { it('starts a search on input change', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) wrapper.vm.handleDebounceSearchInput = (query) => { @@ -44,7 +53,7 @@ describe('Search and filter users', () => { await wrapper.vm.$nextTick() expect(wrapper.vm.usersCount).toEqual(3) - const input = wrapper.find('input.el-input__inner') + const input = wrapper.find('.search input.el-input__inner') input.element.value = 'bob' input.trigger('input') await wrapper.vm.$nextTick() @@ -61,7 +70,10 @@ describe('Search and filter users', () => { it('shows local users on checkbox click', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -82,7 +94,10 @@ describe('Search and filter users', () => { it('shows local users with search query', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) wrapper.vm.handleDebounceSearchInput = (query) => { @@ -131,7 +146,10 @@ describe('Users actions', () => { it('grants admin and moderator rights to a local user', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -153,7 +171,10 @@ describe('Users actions', () => { it('does not show actions that grant admin and moderator rights to external users', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -167,7 +188,10 @@ describe('Users actions', () => { it('toggles activation status', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -185,7 +209,10 @@ describe('Users actions', () => { it('deactivates user when Delete action is called', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -203,7 +230,10 @@ describe('Users actions', () => { it('adds tags', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -227,7 +257,10 @@ describe('Users actions', () => { it('deletes tags', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -245,7 +278,10 @@ describe('Users actions', () => { it('shows check icon when tag is added', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick() @@ -261,7 +297,10 @@ describe('Users actions', () => { it('does not change user index in array when tag is added', async (done) => { const wrapper = mount(Users, { store, - localVue + localVue, + stubs: { + transition: false + } }) await wrapper.vm.$nextTick()