Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Mar 16, 2023
1 parent 8c8037e commit 7a4b86e
Show file tree
Hide file tree
Showing 28 changed files with 441 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
mockAxiosReject,
shallowMount
} from 'web-test-helpers'
import { mock, mockDeep } from 'jest-mock-extended'
import { Graph } from 'web-client'
import { mock } from 'jest-mock-extended'
import { AxiosResponse } from 'axios'

describe('CreateUserModal', () => {
Expand Down Expand Up @@ -45,20 +44,18 @@ describe('CreateUserModal', () => {
})
it('should be false when userName is already existing', async () => {
const { wrapper, mocks } = getWrapper()
const graphMock = mockDeep<Graph>()
const graphMock = mocks.$clientService.graphAuthenticated
const getUserStub = graphMock.users.getUser.mockResolvedValue(
mock<AxiosResponse>({ data: { onPremisesSamAccountName: 'jan' } })
)
mocks.$clientService.graphAuthenticated.mockImplementation(() => graphMock)
wrapper.vm.user.onPremisesSamAccountName = 'jan'
expect(await wrapper.vm.validateUserName()).toBeFalsy()
expect(getUserStub).toHaveBeenCalled()
})
it('should be true when userName is valid', async () => {
const { wrapper, mocks } = getWrapper()
const graphMock = mockDeep<Graph>()
const graphMock = mocks.$clientService.graphAuthenticated
const getUserStub = graphMock.users.getUser.mockRejectedValue(() => mockAxiosReject())
mocks.$clientService.graphAuthenticated.mockImplementation(() => graphMock)
wrapper.vm.user.onPremisesSamAccountName = 'jana'
expect(await wrapper.vm.validateUserName()).toBeTruthy()
expect(getUserStub).toHaveBeenCalled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
mockAxiosReject,
shallowMount
} from 'web-test-helpers'
import { mock, mockDeep } from 'jest-mock-extended'
import { mock } from 'jest-mock-extended'
import { Group } from 'web-client/src/generated'
import { Graph } from 'web-client'
import { AxiosResponse } from 'axios'

const availableGroupOptions = [
Expand Down Expand Up @@ -73,20 +72,18 @@ describe('EditPanel', () => {
})
it('should be false when userName is already existing', async () => {
const { wrapper, mocks } = getWrapper()
const graphMock = mockDeep<Graph>()
const graphMock = mocks.$clientService.graphAuthenticated
const getUserStub = graphMock.users.getUser.mockResolvedValue(
mock<AxiosResponse>({ data: { onPremisesSamAccountName: 'jan' } })
)
mocks.$clientService.graphAuthenticated.mockImplementation(() => graphMock)
wrapper.vm.editUser.onPremisesSamAccountName = 'jan'
expect(await wrapper.vm.validateUserName()).toBeFalsy()
expect(getUserStub).toHaveBeenCalled()
})
it('should be true when userName is valid', async () => {
const { wrapper, mocks } = getWrapper()
const graphMock = mockDeep<Graph>()
const graphMock = mocks.$clientService.graphAuthenticated
const getUserStub = graphMock.users.getUser.mockRejectedValue(() => mockAxiosReject())
mocks.$clientService.graphAuthenticated.mockImplementation(() => graphMock)
wrapper.vm.editUser.onPremisesSamAccountName = 'jana'
expect(await wrapper.vm.validateUserName()).toBeTruthy()
expect(getUserStub).toHaveBeenCalled()
Expand Down
25 changes: 10 additions & 15 deletions packages/web-app-admin-settings/tests/unit/mixins/resetLogo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import resetLogo from '../../../src/mixins/general/resetLogo'
import { mock, mockDeep } from 'jest-mock-extended'
import { clientService } from 'web-pkg'
import { mock } from 'jest-mock-extended'
import {
createStore,
defaultPlugins,
Expand All @@ -22,11 +21,8 @@ const Component = {
describe('resetLogo', () => {
describe('method "$_resetLogo_reset"', () => {
it('should show message on request success', async () => {
const httpClientMock = mockDeep<any>({
delete: jest.fn().mockResolvedValue(() => mockAxiosResolve())
})
jest.spyOn(clientService, 'httpAuthenticated').mockImplementation(() => httpClientMock)
const { wrapper } = getWrapper()
const { wrapper, mocks } = getWrapper()
mocks.$clientService.httpAuthenticated.delete.mockResolvedValue(() => mockAxiosResolve())
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
await wrapper.vm.$_resetLogo_reset()
jest.runAllTimers()
Expand All @@ -36,11 +32,8 @@ describe('resetLogo', () => {

it('should show message on request error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const httpClientMock = mockDeep<any>({
delete: jest.fn().mockRejectedValue(() => mockAxiosReject())
})
jest.spyOn(clientService, 'httpAuthenticated').mockImplementation(() => httpClientMock)
const { wrapper } = getWrapper()
const { wrapper, mocks } = getWrapper()
mocks.$clientService.httpAuthenticated.delete.mockRejectedValue(() => mockAxiosReject())
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
await wrapper.vm.$_resetLogo_reset()
jest.runAllTimers()
Expand All @@ -53,12 +46,14 @@ describe('resetLogo', () => {
function getWrapper() {
const storeOptions = defaultStoreMockOptions
const store = createStore(storeOptions)
const mocks = defaultComponentMocks({
currentRoute: mock<RouteLocation>({ name: 'admin-settings-general' })
})
return {
mocks,
wrapper: mount(Component, {
global: {
mocks: defaultComponentMocks({
currentRoute: mock<RouteLocation>({ name: 'admin-settings-general' })
}),
mocks,
plugins: [...defaultPlugins(), store]
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import uploadLogo from '../../../src/mixins/general/uploadLogo'
import { mock, mockDeep } from 'jest-mock-extended'
import { clientService } from 'web-pkg'
import { mock } from 'jest-mock-extended'
import {
createStore,
defaultPlugins,
Expand All @@ -22,11 +21,8 @@ const Component = {
describe('uploadImage', () => {
describe('method "$_uploadLogo_upload"', () => {
it('should show message on request success', async () => {
const httpClientMock = mockDeep<any>({
post: jest.fn().mockResolvedValue(() => mockAxiosResolve())
})
jest.spyOn(clientService, 'httpAuthenticated').mockImplementation(() => httpClientMock)
const { wrapper } = getWrapper()
const { wrapper, mocks } = getWrapper()
mocks.$clientService.httpAuthenticated.post.mockResolvedValue(() => mockAxiosResolve())
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
await wrapper.vm.$_uploadLogo_upload({
currentTarget: {
Expand All @@ -40,11 +36,8 @@ describe('uploadImage', () => {

it('should show message on request error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const httpClientMock = mockDeep<any>({
post: jest.fn().mockRejectedValue(() => mockAxiosReject())
})
jest.spyOn(clientService, 'httpAuthenticated').mockImplementation(() => httpClientMock)
const { wrapper } = getWrapper()
const { wrapper, mocks } = getWrapper()
mocks.$clientService.httpAuthenticated.post.mockRejectedValue(() => mockAxiosReject())
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
await wrapper.vm.$_uploadLogo_upload({
currentTarget: {
Expand All @@ -58,14 +51,14 @@ describe('uploadImage', () => {

it('should show message on invalid mimeType', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const { wrapper } = getWrapper()
const { wrapper, mocks } = getWrapper()
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
await wrapper.vm.$_uploadLogo_upload({
currentTarget: {
files: [{ name: 'text.txt', type: 'text/plain' }]
}
})
expect(clientService.httpAuthenticated).toHaveBeenCalledTimes(0)
expect(mocks.$clientService.httpAuthenticated.post).toHaveBeenCalledTimes(0)
expect(showMessageStub).toHaveBeenCalledTimes(1)
})
})
Expand All @@ -74,12 +67,14 @@ describe('uploadImage', () => {
function getWrapper() {
const storeOptions = defaultStoreMockOptions
const store = createStore(storeOptions)
const mocks = defaultComponentMocks({
currentRoute: mock<RouteLocation>({ name: 'admin-settings-general' })
})
return {
mocks,
wrapper: mount(Component, {
global: {
mocks: defaultComponentMocks({
currentRoute: mock<RouteLocation>({ name: 'admin-settings-general' })
}),
mocks,
plugins: [...defaultPlugins(), store]
}
})
Expand Down
37 changes: 19 additions & 18 deletions packages/web-app-admin-settings/tests/unit/views/Groups.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Groups from '../../../src/views/Groups.vue'
import { mockAxiosResolve, mockAxiosReject } from 'web-test-helpers/src/mocks'
import { Graph } from 'web-client'
import { mockDeep } from 'jest-mock-extended'
import { ClientService } from 'web-pkg/src'
import {
Expand All @@ -12,10 +11,12 @@ import {
} from 'web-test-helpers'

const selectors = { batchActionsStub: 'batch-actions-stub' }
const defaultGraphMock = () => {
const graph = mockDeep<Graph>()
graph.groups.listGroups.mockImplementation(() => mockAxiosResolve({ value: [{ id: '1' }] }))
return graph
const getClientServiceMock = () => {
const clientService = mockDeep<ClientService>()
clientService.graphAuthenticated.groups.listGroups.mockImplementation(() =>
mockAxiosResolve({ value: [{ id: '1' }] })
)
return clientService
}
jest.mock('web-pkg/src/composables/appDefaults')

Expand All @@ -33,9 +34,11 @@ describe('Groups view', () => {

it('should show message on error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const graph = defaultGraphMock()
graph.groups.createGroup.mockImplementation(() => mockAxiosReject())
const { wrapper } = getWrapper({ graph })
const clientService = getClientServiceMock()
clientService.graphAuthenticated.groups.createGroup.mockImplementation(() =>
mockAxiosReject()
)
const { wrapper } = getWrapper({ clientService })
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
const toggleCreateGroupModalStub = jest.spyOn(wrapper.vm, 'toggleCreateGroupModal')
await wrapper.vm.createGroup({ displayName: 'admins' })
Expand All @@ -56,9 +59,9 @@ describe('Groups view', () => {

it('should show message on error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const graph = defaultGraphMock()
graph.groups.editGroup.mockImplementation(() => mockAxiosReject())
const { wrapper } = getWrapper({ graph })
const clientService = getClientServiceMock()
clientService.graphAuthenticated.groups.editGroup.mockImplementation(() => mockAxiosReject())
const { wrapper } = getWrapper({ clientService })
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
await wrapper.vm.editGroup({})

Expand Down Expand Up @@ -101,11 +104,11 @@ describe('Groups view', () => {
expect(wrapper.vm.allGroupsSelected).toBeTruthy()
})
it('should be false if not every group is selected', async () => {
const graph = defaultGraphMock()
graph.groups.listGroups.mockImplementation(() =>
const clientService = getClientServiceMock()
clientService.graphAuthenticated.groups.listGroups.mockImplementation(() =>
mockAxiosResolve({ value: [{ id: '1' }, { id: '2' }] })
)
const { wrapper } = getWrapper({ graph })
const { wrapper } = getWrapper({ clientService })
wrapper.vm.selectedGroups = [{ id: '1' }]
await wrapper.vm.loadResourcesTask.last
expect(wrapper.vm.allGroupsSelected).toBeFalsy()
Expand Down Expand Up @@ -135,10 +138,8 @@ describe('Groups view', () => {
})
})

function getWrapper({ graph = defaultGraphMock() } = {}) {
const $clientService = mockDeep<ClientService>()
$clientService.graphAuthenticated.mockImplementation(() => graph)
const mocks = { ...defaultComponentMocks(), $clientService }
function getWrapper({ clientService = getClientServiceMock() } = {}) {
const mocks = { ...defaultComponentMocks(), $clientService: clientService }
const storeOptions = { ...defaultStoreMockOptions }
const store = createStore(storeOptions)
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ describe('Spaces view', () => {
})

function getWrapper({ spaces = [{ name: 'Some Space' }] } = {}) {
const graph = mockDeep<Graph>()
graph.drives.listAllDrives.mockImplementation(() => mockAxiosResolve({ value: spaces }))
const $clientService = mockDeep<ClientService>()
$clientService.graphAuthenticated.mockImplementation(() => graph)
$clientService.graphAuthenticated.drives.listAllDrives.mockImplementation(() =>
mockAxiosResolve({ value: spaces })
)
const mocks = {
...defaultComponentMocks(),
$clientService,
Expand Down
Loading

0 comments on commit 7a4b86e

Please sign in to comment.