Skip to content

Commit

Permalink
Vue 3: Get rid of Vue default import/export usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dschmidt committed Jan 20, 2023
1 parent 01d4fd7 commit 6d19192
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallowMount } from 'web-test-helpers/src'
import { defaultPlugins, shallowMount } from 'web-test-helpers/src'
import OcButton from './OcButton.vue'

describe('OcButton', () => {
Expand Down Expand Up @@ -143,9 +143,9 @@ describe('OcButton', () => {
})

function getWrapperWithProps(props) {
return shallowMount(OcButton, { props })
return shallowMount(OcButton, { props, global: { plugins: [...defaultPlugins()] } })
}
function getWrapperWithTestSlot() {
const testSlots = { default: '<p class="text">Test button</p>' }
return shallowMount(OcButton, { slots: testSlots })
return shallowMount(OcButton, { slots: testSlots, global: { plugins: [...defaultPlugins()] } })
}
13 changes: 10 additions & 3 deletions packages/design-system/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue from 'vue'
import { createApp, defineComponent } from 'vue'
import DesignSystem from './index'

const options = {
Expand All @@ -22,10 +22,17 @@ const options = {
}
}

Vue.use(DesignSystem, options)

describe('Depending on what gets passed into the theming options', () => {
it('Sets correct custom CSS props from theming options', () => {
const app = createApp(
defineComponent({
template: '<div/>'
})
)
app.config.compilerOptions.whitespace = 'preserve'
app.use(DesignSystem, options)
app.mount('body')

expect(document.documentElement.style.getPropertyValue('--oc-breakpoint-xsmall-max')).toMatch(
'50px'
)
Expand Down
6 changes: 3 additions & 3 deletions packages/design-system/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ const initializeCustomProps = (tokens = [], prefix) => {
}

export default {
install(Vue, options: any = {}) {
install(app, options: any = {}) {
const themeOptions = options.tokens
initializeCustomProps(themeOptions?.breakpoints, 'breakpoint-')
initializeCustomProps(themeOptions?.colorPalette, 'color-')
initializeCustomProps(themeOptions?.fontSizes, 'font-size-')
initializeCustomProps(themeOptions?.sizes, 'size-')
initializeCustomProps(themeOptions?.spacing, 'space-')

Object.values(components).forEach((c) => Vue.component(c.name, c))
Object.values(directives).forEach((d) => Vue.directive(d.name, d))
Object.values(components).forEach((c) => app.component(c.name, c))
Object.values(directives).forEach((d) => app.directive(d.name, d))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import SpacesList from '../../../../src/components/Spaces/SpacesList.vue'
import { defaultPlugins, mount, shallowMount } from 'web-test-helpers'
import Vue from 'vue'
import { displayPositionedDropdown, eventBus } from 'web-pkg'
import { SideBarEventTopics } from 'web-pkg/src/composables/sideBar'
import { nextTick } from 'vue'

const spaceMocks = [
{
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('SpacesList', () => {
it('shows only filtered spaces if filter applied', async () => {
const { wrapper } = getWrapper({ spaces: spaceMocks })
wrapper.vm.filterTerm = 'Another'
await Vue.nextTick()
await nextTick()
expect(wrapper.vm.orderedSpaces).toEqual([spaceMocks[1]])
})
it('should show the context menu on right click', async () => {
Expand Down
24 changes: 12 additions & 12 deletions packages/web-app-files/src/router/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue, { ComponentOptions } from 'vue'
import { ComponentOptions } from 'vue'

/**
* we need to inject the vue files into the route builders,
Expand All @@ -7,19 +7,19 @@ import Vue, { ComponentOptions } from 'vue'
* into js files which then again get imported by other vue files...
*/
export interface RouteComponents {
App: ComponentOptions<typeof Vue>
Favorites: ComponentOptions<typeof Vue>
FilesDrop: ComponentOptions<typeof Vue>
PrivateLink: ComponentOptions<typeof Vue>
SearchResults: ComponentOptions<typeof Vue>
PublicLink: ComponentOptions<typeof Vue>
App: ComponentOptions<unknown>
Favorites: ComponentOptions<unknown>
FilesDrop: ComponentOptions<unknown>
PrivateLink: ComponentOptions<unknown>
SearchResults: ComponentOptions<unknown>
PublicLink: ComponentOptions<unknown>
Shares: {
SharedWithMe: ComponentOptions<typeof Vue>
SharedWithOthers: ComponentOptions<typeof Vue>
SharedViaLink: ComponentOptions<typeof Vue>
SharedWithMe: ComponentOptions<unknown>
SharedWithOthers: ComponentOptions<unknown>
SharedViaLink: ComponentOptions<unknown>
}
Spaces: {
DriveResolver: ComponentOptions<typeof Vue>
Projects: ComponentOptions<typeof Vue>
DriveResolver: ComponentOptions<unknown>
Projects: ComponentOptions<unknown>
}
}
6 changes: 3 additions & 3 deletions packages/web-app-preview/src/tests/unit/views/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import App from '../../../App.vue'
import Vue, { ref } from 'vue'
import { nextTick, ref } from 'vue'
import {
createStore,
defaultComponentMocks,
Expand Down Expand Up @@ -81,12 +81,12 @@ describe('Preview app', () => {
describe('Method "preloadImages"', () => {
it('should preload images if active file changes', async () => {
const { wrapper } = createShallowMountWrapper()
await Vue.nextTick()
await nextTick()

wrapper.vm.toPreloadImageIds = []
wrapper.vm.setActiveFile('personal/admin/sleeping_dog.gif')

await Vue.nextTick()
await nextTick()

expect(wrapper.vm.toPreloadImageIds).toEqual(['8', '9', '1', '6', '4'])
})
Expand Down
4 changes: 1 addition & 3 deletions packages/web-runtime/src/container/application/classic.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { ClassicApplicationScript, RuntimeApi } from '../types'
import { buildRuntimeApi } from '../api'
import Vue, { App } from 'vue'
import { App } from 'vue'
import { isFunction, isObject } from 'lodash-es'
import { NextApplication } from './next'
import { Store } from 'vuex'
import { Router } from 'vue-router'
import { RuntimeError } from '../error'

type VueConstructor = typeof Vue

/**
* this wraps a classic application structure into a next application format.
* it is fully backward compatible and will stay around as a fallback.
Expand Down
4 changes: 1 addition & 3 deletions packages/web-runtime/src/container/application/next.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Vue, { App } from 'vue'
import { App } from 'vue'
import { RuntimeApi } from '../types'

type VueConstructor = typeof Vue
export abstract class NextApplication {
protected readonly runtimeApi: RuntimeApi

Expand Down
1 change: 0 additions & 1 deletion packages/web-runtime/src/defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { coreTranslations, clientTranslations, pkgTranslations, odsTranslations
// fontawesome-free attributions console message
import '@fortawesome/fontawesome-free/attribution'

export { default as Vue } from './vue'
export { default as DesignSystem } from '@ownclouders/design-system'

export const pages = { success: App, failure: missingOrInvalidConfigPage }
Expand Down
19 changes: 0 additions & 19 deletions packages/web-runtime/src/defaults/vue.js

This file was deleted.

14 changes: 12 additions & 2 deletions packages/web-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
pages,
translations,
supportedLanguages,
Vue
} from './defaults'

import { router } from './router'
Expand Down Expand Up @@ -40,13 +39,19 @@ import { configureCompat, createApp } from 'vue'
import { compatConfig } from './compatConfig'
import PortalVue, { createWormhole } from 'portal-vue'

import WebPlugin from './plugins/web'
import Avatar from './components/Avatar.vue'
import focusMixin from './mixins/focusMixin'
import lifecycleMixin from './mixins/lifecycleMixin'


configureCompat(compatConfig)

export const bootstrapApp = async (configurationPath: string): Promise<void> => {
const app = createApp(pages.success)

const runtimeConfiguration = await announceConfiguration(configurationPath)
startSentry(runtimeConfiguration, Vue)
startSentry(runtimeConfiguration, app)

const store = await announceStore({ runtimeConfiguration })
announcePermissionManager({ app, store })
Expand Down Expand Up @@ -76,6 +81,11 @@ export const bootstrapApp = async (configurationPath: string): Promise<void> =>
wormhole: app.config.globalProperties.$wormhole
})

app.use(WebPlugin)
app.component('AvatarImage', Avatar)
app.mixin(focusMixin)
app.mixin(lifecycleMixin)

app.mount('#owncloud')

const applications = Array.from(applicationStore.values())
Expand Down
4 changes: 2 additions & 2 deletions packages/web-runtime/src/plugins/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { encodePath } from 'web-pkg/src/utils'
import { v4 as uuidV4 } from 'uuid'

export default {
install(Vue) {
Vue.mixin({
install(app) {
app.mixin({
computed: {
...mapGetters(['capabilities']),

Expand Down
33 changes: 30 additions & 3 deletions packages/web-test-helpers/src/defaultPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import DesignSystem from '@ownclouders/design-system'
import Vue from 'vue'
import { createGettext } from 'vue3-gettext'
;(window as any).Vue = Vue

import { h } from 'vue'
export interface DefaultPluginsOptions {
designSystem?: boolean
gettext?: boolean
Expand Down Expand Up @@ -31,5 +29,34 @@ export const defaultPlugins = ({
})
}

plugins.push({
install(app) {
app.component('RouterLink', {
name: 'RouterLink',
props: {
tag: { type: String, default: 'a' },
to: { type: [String, Object], default: '' }
},
render() {
let path = this.$props.to

if (!!path && typeof path !== 'string') {
path = this.$props.to.path || this.$props.to.name

if (this.$props.to.params) {
path += '/' + Object.values(this.$props.to.params).join('/')
}

if (this.$props.to.query) {
path += '?' + Object.values(this.$props.to.query).join('&')
}
}

return h(this.tag, { attrs: { href: path } }, this.$slots.default)
}
})
}
})

return plugins
}
27 changes: 1 addition & 26 deletions tests/unit/config/jest.init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue, { configureCompat, h } from 'vue'
import { configureCompat } from 'vue'
import fetchMock from 'jest-fetch-mock'

import { compatConfig } from '../../../packages/web-runtime/src/compatConfig'
Expand All @@ -12,28 +12,3 @@ window.IntersectionObserver = jest.fn(() => ({
unobserve: jest.fn()
}))
fetchMock.enableMocks()

Vue.component('RouterLink', {
name: 'RouterLink',
props: {
tag: { type: String, default: 'a' },
to: { type: [String, Object], default: '' }
},
render() {
let path = this.$props.to

if (!!path && typeof path !== 'string') {
path = this.$props.to.path || this.$props.to.name

if (this.$props.to.params) {
path += '/' + Object.values(this.$props.to.params).join('/')
}

if (this.$props.to.query) {
path += '?' + Object.values(this.$props.to.query).join('&')
}
}

return h(this.tag, { attrs: { href: path } }, this.$slots.default)
}
})

0 comments on commit 6d19192

Please sign in to comment.