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

feat: include the mocks in component globalThis #2120

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 9 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ export default {
}
}
</script>

// Or if you are using script setup

<script setup>
function onClick() {
$store.dispatch('click')
}
</script>

```

`Component.spec.js`:
Expand Down
2 changes: 2 additions & 0 deletions src/createInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ export function createInstance(
this.$.setupState[k] = v
// eslint-disable-next-line no-empty
} catch (e) {}
//@ts-ignore Setting an unknown global variable for the component instance
globalThis[k] = v
Copy link
Member

Choose a reason for hiding this comment

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

Did this work in Test Utils v1? I actually am surprised this doesn't "just work".

}
// also intercept the proxy calls to make the mocks available on the instance
// (useful when a template access a global function like $t and the developer wants to mock it)
Expand Down
16 changes: 16 additions & 0 deletions tests/components/ScriptSetupWithGlobalFunction.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script setup lang="ts">
import { ref } from 'vue'
const hello = ref('hello')

function clicked() {
//@ts-ignore For example Nuxt will inject these kinds of global functions
$fetch('http://www.example.com').then((response : any) => {
hello.value = response.data
})
}

</script>

<template>
<button @click="clicked">{{ hello }}</button>
</template>
14 changes: 14 additions & 0 deletions tests/mountingOptions/mocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest'
import { mount, RouterLinkStub } from '../../src'
import { defineComponent } from 'vue'
import ScriptSetupWithI18n from '../components/ScriptSetupWithI18n.vue'
import ScriptSetupWithGlobalFunction from '../components/ScriptSetupWithGlobalFunction.vue'
import ComponentWithI18n from '../components/ComponentWithI18n.vue'

describe('mocks', () => {
Expand Down Expand Up @@ -90,6 +91,19 @@ describe('mocks', () => {
expect(wrapper.text()).toContain('mocked')
})

it('mocks a global function used in a script setup', async () => {
const wrapper = mount(ScriptSetupWithGlobalFunction, {
global: {
mocks: {
$fetch: async (url: string) => ({ data: 'mocked' })
}
}
})
expect(wrapper.text()).toContain('hello')
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('mocked')
})

it('mocks a global function in an option component', () => {
const wrapper = mount(ComponentWithI18n, {
global: {
Expand Down