Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Nov 25, 2022
1 parent 4236365 commit 1f402b8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import Vue from 'vue'
const localVue = createLocalVue()
localVue.use(Vuex)

export const createComposableWrapper = (setup: SetupFunction<Data, Data>): Wrapper<Vue> =>
export const createComposableWrapper = (
setup: SetupFunction<Data, Data>,
options = { mocks: undefined, store: undefined }
): Wrapper<Vue> =>
mount(
defineComponent({
setup,
template: `<div></div>`
}),
{
localVue
localVue,
...(options.mocks && { mocks: options.mocks }),
...(options.store && { store: options.store })
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { mockDeep } from 'jest-mock-extended'
import { useScrollTo } from 'web-app-files/src/composables/scrollTo'
import { Resource } from 'web-client/src'
import { eventBus } from 'web-pkg/src'
import { defaultComponentMocks } from 'web-test-helpers/src/mocks/defaultComponentMocks'
import { defaultStoreMockOptions } from 'web-test-helpers/src/mocks/store/defaultStoreMockOptions'
import { createComposableWrapper } from '../composables.setup'

describe('useScrollTo', () => {
it('should be valid', () => {
expect(useScrollTo).toBeDefined()
})
describe('method "scrollToResource"', () => {
const getHTMLPageObject = () => ({
getBoundingClientRect: jest.fn(() => ({ bottom: 300, top: 0 })),
scrollIntoView: jest.fn(),
scrollBy: jest.fn(),
offsetHeight: 100
})

it('calls "scrollIntoView" when the page bottom is reached', () => {
const htmlPageObject = getHTMLPageObject()
jest.spyOn(document, 'querySelectorAll').mockImplementation(() => [htmlPageObject] as any)
window.innerHeight = 100

createComposableWrapper(
() => {
const { scrollToResource } = useScrollTo()
scrollToResource(mockDeep<Resource>())
expect(htmlPageObject.scrollIntoView).toHaveBeenCalled()
},
{ mocks: defaultComponentMocks(), store: defaultStoreMockOptions }
)
})
it('calls "scrollBy" when the page top is reached', () => {
const htmlPageObject = getHTMLPageObject()
jest.spyOn(document, 'querySelectorAll').mockImplementation(() => [htmlPageObject] as any)
jest
.spyOn(document, 'getElementsByClassName')
.mockImplementation(() => [htmlPageObject] as any)
window.innerHeight = 500

createComposableWrapper(
() => {
const { scrollToResource } = useScrollTo()
scrollToResource(mockDeep<Resource>())
expect(htmlPageObject.scrollBy).toHaveBeenCalled()
},
{ mocks: defaultComponentMocks(), store: defaultStoreMockOptions }
)
})
})
describe('method "scrollToResourceFromRoute"', () => {
const resourceId = 'someFileId'

it('does not scroll without the "scrollTo" param', () => {
createComposableWrapper(
() => {
const resource = mockDeep<Resource>({ id: resourceId })
const { scrollToResourceFromRoute } = useScrollTo()
const querySelectorAllSpy = jest.spyOn(document, 'querySelectorAll')
scrollToResourceFromRoute([resource])
expect(querySelectorAllSpy).not.toHaveBeenCalled()
},
{
mocks: { ...defaultComponentMocks() },
store: defaultStoreMockOptions
}
)
})
it('does not scroll when no resource found', () => {
createComposableWrapper(
() => {
const resource = mockDeep<Resource>({ id: 'someOtherFileId' })
const { scrollToResourceFromRoute } = useScrollTo()
const querySelectorAllSpy = jest.spyOn(document, 'querySelectorAll')
scrollToResourceFromRoute([resource])
expect(querySelectorAllSpy).not.toHaveBeenCalled()
},
{
mocks: {
...defaultComponentMocks({ currentRoute: { query: { scrollTo: resourceId } } })
},
store: defaultStoreMockOptions
}
)
})
it('scrolls to the resource when the "scrollTo" param is given and a resource is found', () => {
createComposableWrapper(
() => {
const resource = mockDeep<Resource>({ id: resourceId })
const { scrollToResourceFromRoute } = useScrollTo()
const querySelectorAllSpy = jest.spyOn(document, 'querySelectorAll')
scrollToResourceFromRoute([resource])
expect(querySelectorAllSpy).toHaveBeenCalled()
},
{
mocks: {
...defaultComponentMocks({
currentRoute: { query: { scrollTo: resourceId } }
}),
$store: { commit: jest.fn() }
},
store: defaultStoreMockOptions
}
)
})
it('opens the sidebar when a resource is found and the "details" param is given', () => {
createComposableWrapper(
() => {
const busStub = jest.spyOn(eventBus, 'publish')
const resource = mockDeep<Resource>({ id: resourceId })
const { scrollToResourceFromRoute } = useScrollTo()
scrollToResourceFromRoute([resource])
expect(busStub).toHaveBeenCalled()
},
{
mocks: {
...defaultComponentMocks({
currentRoute: { query: { scrollTo: resourceId, details: 'details-item' } }
}),
$store: { commit: jest.fn() }
},
store: defaultStoreMockOptions
}
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { filesModuleMockOptions } from './filesModuleMockOptions'
import { runtimeModuleMockOptions } from './runtimeModuleMockOptions'

export const defaultStoreMockOptions = {
commit: jest.fn(),
getters: {
capabilities: jest.fn().mockImplementation(() => ({})),
configuration: jest
Expand Down

0 comments on commit 1f402b8

Please sign in to comment.