Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests-only] Add unit tests for the text editor #8002

Merged
merged 3 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/web-app-text-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"vue-gettext": "^2.1.5",
"vuex": "^3.6.2",
"web-client": "npm:@ownclouders/web-client",
"web-pkg": "npm:@ownclouders/web-pkg"
"web-pkg": "npm:@ownclouders/web-pkg",
"vuex-extensions": "^1.1.5"
}
}
69 changes: 69 additions & 0 deletions packages/web-app-text-editor/tests/unit/app.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import App from '../../src/App.vue'
import { mount } from '@vue/test-utils'
import Vuex from 'vuex'
import { defaultComponentMocks } from 'web-test-helpers/src/mocks/defaultComponentMocks'
import { defaultLocalVue } from 'web-test-helpers/src/localVue/defaultLocalVue'
import { defaultStoreMockOptions } from 'web-test-helpers/src/mocks/store/defaultStoreMockOptions'
import { createStore } from 'vuex-extensions'
import { defaultStubs } from 'web-test-helpers/src/mocks/defaultStubs'
import { useAppDefaultsMock } from 'web-test-helpers/src/mocks/useAppDefaultsMock'
import { FileContext, useAppDefaults } from 'web-pkg/src/composables/appDefaults'
import { mockDeep } from 'jest-mock-extended'
import { ref } from '@vue/composition-api'

jest.mock('web-pkg/src/composables/appDefaults')

describe('Text editor app', () => {
it('appTopBar always present', () => {
const { wrapper } = getWrapper()
expect(wrapper.find('app-top-bar-stub').exists()).toBeTruthy()
})
describe('different view states', () => {
it('shows the loading spinner during loading', () => {
const { wrapper } = getWrapper()
expect(wrapper.find('oc-spinner-stub').exists()).toBeTruthy()
})
it('shows the editor after loading', async () => {
const { wrapper } = getWrapper()
await wrapper.vm.loadFileTask.last
expect(wrapper.find('oc-spinner-stub').exists()).toBeFalsy()
expect(wrapper.find('oc-textarea-stub').exists()).toBeTruthy()
})
})
describe('preview', () => {
it.each([
{ fileExtension: 'txt', showPreview: false },
{ fileExtension: 'js', showPreview: false },
{ fileExtension: 'php', showPreview: false },
{ fileExtension: 'json', showPreview: false },
{ fileExtension: 'xml', showPreview: false },
{ fileExtension: 'md', showPreview: true }
])('shows only for supported file types', async (data) => {
const { wrapper } = getWrapper({ fileName: `file.${data.fileExtension}` })
await wrapper.vm.loadFileTask.last
expect(wrapper.find('#text-editor-preview').exists()).toBe(data.showPreview)
})
})
})

function getWrapper({ fileName = 'someFile.txt' }: { fileName?: string } = {}) {
jest.mocked(useAppDefaults).mockImplementation(() =>
useAppDefaultsMock({
currentFileContext: ref(mockDeep<FileContext>({ path: fileName }))
})
)
const defaultMocks = { ...defaultComponentMocks() }
const storeOptions = { ...defaultStoreMockOptions }
const localVue = defaultLocalVue()
const store = createStore(Vuex.Store, storeOptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope for this PR, but:

I don't like that you use options from test helpers but have to add vuex-extensions to the package dependencies, I would like to see this handled in web-test-helpers completely.
We should really throw some time at unit tests at some point, unify the setup and clean up stuff like this.

return {
mocks: defaultMocks,
storeOptions,
wrapper: mount(App, {
localVue,
mocks: defaultMocks,
store,
stubs: defaultStubs
})
}
}
2 changes: 1 addition & 1 deletion packages/web-client/src/webdav/getFileContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { urlJoin } from '../utils'
import { isPublicSpaceResource, SpaceResource } from '../helpers'
import { WebDavOptions } from './types'

type GetFileContentsResponse = {
export type GetFileContentsResponse = {
body: any
[key: string]: any
}
Expand Down
4 changes: 3 additions & 1 deletion packages/web-test-helpers/src/mocks/defaultStubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export const defaultStubs = {
'generic-trash': true,
'generic-space': true,
'space-header': true,
'shared-with-me-section': true
'shared-with-me-section': true,
'app-top-bar': true,
'oc-textarea': true
}
29 changes: 29 additions & 0 deletions packages/web-test-helpers/src/mocks/useAppDefaultsMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { mockDeep } from 'jest-mock-extended'
import { ref } from '@vue/composition-api'
import { Resource } from 'web-client/src'
import { FileResource } from 'web-client/src/helpers'
import { GetFileContentsResponse } from 'web-client/src/webdav/getFileContents'
import { AppConfigObject, FileContext, useAppDefaults } from 'web-pkg/src'

export const useAppDefaultsMock = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still amazed by the mocks with the partial type for options 😎

options: Partial<ReturnType<typeof useAppDefaults>> = {}
): ReturnType<typeof useAppDefaults> => {
return {
isPublicLinkContext: ref(false),
currentFileContext: ref(mockDeep<FileContext>()),
applicationConfig: ref(mockDeep<AppConfigObject>()),
closeApp: jest.fn(),
replaceInvalidFileRoute: jest.fn(),
getUrlForResource: jest.fn(),
revokeUrl: jest.fn(),
getFileInfo: jest.fn().mockImplementation(() => mockDeep<Resource>()),
getFileContents: jest
.fn()
.mockImplementation(() => mockDeep<GetFileContentsResponse>({ body: '' })),
putFileContents: jest.fn().mockImplementation(() => mockDeep<FileResource>()),
isFolderLoading: ref(false),
activeFiles: ref([]),
loadFolderForFileContext: jest.fn(),
...options
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.