Skip to content

Commit

Permalink
fix vue warnings and cover non setup initializations
Browse files Browse the repository at this point in the history
  • Loading branch information
Maronato committed Oct 17, 2021
1 parent 8b15bd4 commit 074ceb8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ const VueToastificationPlugin: Plugin = (App, options?: PluginOptions) => {

const provideToast = (options?: PluginOptions) => {
const toast = ownExports.createToastInterface(options)
provide(toastInjectionKey, toast)
if (getCurrentInstance()) {
provide(toastInjectionKey, toast)
}
}

const useToast = (eventBus?: EventBus) => {
if (eventBus) {
return ownExports.createToastInterface(eventBus)
}
const toast = getCurrentInstance() ? inject(toastInjectionKey) : undefined
const toast = getCurrentInstance()
? inject(toastInjectionKey, undefined)
: undefined
return toast ? toast : ownExports.createToastInterface(globalEventBus)
}

Expand Down
35 changes: 26 additions & 9 deletions tests/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ const Consumer = {
},
}

const Provider = defineComponent({
setup() {
index.provideToast()
return () => vue.h(Consumer)
},
})
const createProvider = (options?: index.PluginOptions) =>
defineComponent({
setup() {
index.provideToast(options)
return () => vue.h(Consumer)
},
})

const Provider = createProvider()

const Middle = {
render: () => vue.h(Consumer),
Expand Down Expand Up @@ -123,7 +126,7 @@ describe("createToastInterface", () => {
})
it("uses mock if not in browser", () => {
const isBrowserSpy = jest.spyOn(utils, "isBrowser")
const consoleSpy = jest.spyOn(console, "warn")
const consoleSpy = jest.spyOn(console, "warn").mockImplementation()
isBrowserSpy.mockReturnValueOnce(false)

const toast = index.createToastInterface()
Expand All @@ -149,7 +152,7 @@ describe("provideToast", () => {
expect(provideSpy).not.toHaveBeenCalled()
expect(createToastInterfaceSpy).not.toHaveBeenCalled()

index.provideToast()
mount(Provider)

expect(provideSpy).toHaveBeenCalledTimes(1)
expect(provideSpy).toHaveBeenCalledWith(
Expand All @@ -167,7 +170,7 @@ describe("provideToast", () => {
expect(provideSpy).not.toHaveBeenCalled()
expect(createToastInterfaceSpy).not.toHaveBeenCalled()

index.provideToast({ maxToasts: 10, timeout: 100 })
mount(createProvider({ maxToasts: 10, timeout: 100 }))

expect(provideSpy).toHaveBeenCalledTimes(1)
expect(provideSpy).toHaveBeenCalledWith(
Expand All @@ -182,6 +185,20 @@ describe("provideToast", () => {
})
)
})

it("does not provide if not in setup", () => {
const provideSpy = jest.spyOn(vue, "provide")
const createToastInterfaceSpy = jest.spyOn(index, "createToastInterface")

expect(provideSpy).not.toHaveBeenCalled()
expect(createToastInterfaceSpy).not.toHaveBeenCalled()

index.provideToast()

expect(provideSpy).toHaveBeenCalledTimes(0)
expect(createToastInterfaceSpy).toHaveBeenCalledTimes(1)
expect(createToastInterfaceSpy).toHaveBeenCalledWith(undefined)
})
})

describe("useToast", () => {
Expand Down

0 comments on commit 074ceb8

Please sign in to comment.