mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 11:40:56 +08:00

* Tidy up runtime JS * Initial implementation of runtime over http * Update runtime deps. Fix test task. * Support Clipboard. Message Processor refactor. * Add `Window.Screen()` Clipboard `GetText` -> `Text` * Support most dialogs Better JS->Go object mapping Implement Go->JS callback mechanism Rename `window.runtime` -> `window.wails` to better reflect the Go API * Support SaveFile dialog * Remove go.work * Tidy up * Event->CustomEvent to prevent potential clash with native JS Event object Support Eventing * Support application calls * Support logging * Support named windows Remove debug info * Update v3 changes
116 lines
3.2 KiB
JavaScript
116 lines
3.2 KiB
JavaScript
import { On, Off, OffAll, OnMultiple, CustomEvent, dispatchCustomEvent, eventListeners, Once } from './events'
|
|
import { expect, describe, it, vi, afterEach, beforeEach } from 'vitest'
|
|
|
|
afterEach(() => {
|
|
OffAll();
|
|
vi.resetAllMocks()
|
|
})
|
|
|
|
describe('OnMultiple', () => {
|
|
let testEvent = new CustomEvent('a', {})
|
|
|
|
it('should stop after a specified number of times', () => {
|
|
const cb = vi.fn()
|
|
OnMultiple('a', cb, 5)
|
|
dispatchCustomEvent(testEvent)
|
|
dispatchCustomEvent(testEvent)
|
|
dispatchCustomEvent(testEvent)
|
|
dispatchCustomEvent(testEvent)
|
|
dispatchCustomEvent(testEvent)
|
|
dispatchCustomEvent(testEvent)
|
|
expect(cb).toBeCalledTimes(5);
|
|
})
|
|
|
|
it('should return a cancel fn', () => {
|
|
const cb = vi.fn()
|
|
const cancel = OnMultiple('a', cb, 5)
|
|
dispatchCustomEvent(testEvent)
|
|
dispatchCustomEvent(testEvent)
|
|
cancel()
|
|
dispatchCustomEvent(testEvent)
|
|
dispatchCustomEvent(testEvent)
|
|
expect(cb).toBeCalledTimes(2)
|
|
})
|
|
})
|
|
|
|
describe('On', () => {
|
|
it('should create a listener with a count of -1', () => {
|
|
On('a', () => {})
|
|
expect(eventListeners.get("a")[0].maxCallbacks).toBe(-1)
|
|
})
|
|
|
|
it('should return a cancel fn', () => {
|
|
const cancel = On('a', () => {})
|
|
cancel();
|
|
})
|
|
})
|
|
|
|
describe('Once', () => {
|
|
it('should create a listener with a count of 1', () => {
|
|
Once('a', () => {})
|
|
expect(eventListeners.get("a")[0].maxCallbacks).toBe(1)
|
|
})
|
|
|
|
it('should return a cancel fn', () => {
|
|
const cancel = EventsOn('a', () => {})
|
|
cancel();
|
|
})
|
|
})
|
|
//
|
|
// describe('EventsNotify', () => {
|
|
// it('should inform a listener', () => {
|
|
// const cb = vi.fn()
|
|
// EventsOn('a', cb)
|
|
// EventsNotify(JSON.stringify({name: 'a', data: ["one", "two", "three"]}))
|
|
// expect(cb).toBeCalledTimes(1);
|
|
// expect(cb).toHaveBeenLastCalledWith("one", "two", "three");
|
|
// expect(window.WailsInvoke).toBeCalledTimes(0);
|
|
// })
|
|
// })
|
|
//
|
|
// describe('EventsEmit', () => {
|
|
// it('should emit an event', () => {
|
|
// EventsEmit('a', 'one', 'two', 'three')
|
|
// expect(window.WailsInvoke).toBeCalledTimes(1);
|
|
// const calledWith = window.WailsInvoke.calls[0][0];
|
|
// expect(calledWith.slice(0, 2)).toBe('EE')
|
|
// expect(JSON.parse(calledWith.slice(2))).toStrictEqual({data: ["one", "two", "three"], name: "a"})
|
|
// })
|
|
// })
|
|
//
|
|
describe('Off', () => {
|
|
beforeEach(() => {
|
|
On('a', () => {})
|
|
On('a', () => {})
|
|
On('a', () => {})
|
|
On('b', () => {})
|
|
On('c', () => {})
|
|
})
|
|
|
|
it('should cancel all event listeners for a single type', () => {
|
|
Off('a')
|
|
expect(eventListeners.get('a')).toBeUndefined()
|
|
expect(eventListeners.get('b')).not.toBeUndefined()
|
|
expect(eventListeners.get('c')).not.toBeUndefined()
|
|
})
|
|
|
|
it('should cancel all event listeners for multiple types', () => {
|
|
Off('a', 'b')
|
|
expect(eventListeners.get('a')).toBeUndefined()
|
|
expect(eventListeners.get('b')).toBeUndefined()
|
|
expect(eventListeners.get('c')).not.toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('OffAll', () => {
|
|
it('should cancel all event listeners', () => {
|
|
On('a', () => {})
|
|
On('a', () => {})
|
|
On('a', () => {})
|
|
On('b', () => {})
|
|
On('c', () => {})
|
|
OffAll()
|
|
expect(eventListeners.size).toBe(0)
|
|
})
|
|
})
|