github.com/secoba/wails/v2@v2.6.4/internal/frontend/runtime/desktop/events.test.js (about) 1 import { EventsOnMultiple, EventsNotify, eventListeners, EventsOn, EventsEmit, EventsOffAll, EventsOnce, EventsOff } from './events' 2 import { expect, describe, it, beforeAll, vi, afterEach, beforeEach } from 'vitest' 3 // Edit an assertion and save to see HMR in action 4 5 beforeAll(() => { 6 window.WailsInvoke = vi.fn(() => {}) 7 }) 8 9 afterEach(() => { 10 EventsOffAll(); 11 vi.resetAllMocks() 12 }) 13 14 describe('EventsOnMultiple', () => { 15 it('should stop after a specified number of times', () => { 16 const cb = vi.fn() 17 EventsOnMultiple('a', cb, 5) 18 EventsNotify(JSON.stringify({name: 'a', data: {}})) 19 EventsNotify(JSON.stringify({name: 'a', data: {}})) 20 EventsNotify(JSON.stringify({name: 'a', data: {}})) 21 EventsNotify(JSON.stringify({name: 'a', data: {}})) 22 EventsNotify(JSON.stringify({name: 'a', data: {}})) 23 EventsNotify(JSON.stringify({name: 'a', data: {}})) 24 expect(cb).toBeCalledTimes(5); 25 expect(window.WailsInvoke).toBeCalledTimes(1); 26 expect(window.WailsInvoke).toHaveBeenLastCalledWith('EXa'); 27 }) 28 29 it('should return a cancel fn', () => { 30 const cb = vi.fn() 31 const cancel = EventsOnMultiple('a', cb, 5) 32 EventsNotify(JSON.stringify({name: 'a', data: {}})) 33 EventsNotify(JSON.stringify({name: 'a', data: {}})) 34 cancel() 35 EventsNotify(JSON.stringify({name: 'a', data: {}})) 36 EventsNotify(JSON.stringify({name: 'a', data: {}})) 37 expect(cb).toBeCalledTimes(2) 38 expect(window.WailsInvoke).toBeCalledTimes(1); 39 expect(window.WailsInvoke).toHaveBeenLastCalledWith('EXa'); 40 }) 41 }) 42 43 describe('EventsOn', () => { 44 it('should create a listener with a count of -1', () => { 45 EventsOn('a', () => {}) 46 expect(eventListeners['a'][0].maxCallbacks).toBe(-1) 47 }) 48 49 it('should return a cancel fn', () => { 50 const cancel = EventsOn('a', () => {}) 51 cancel(); 52 expect(window.WailsInvoke).toBeCalledTimes(1); 53 expect(window.WailsInvoke).toHaveBeenLastCalledWith('EXa'); 54 }) 55 }) 56 57 describe('EventsOnce', () => { 58 it('should create a listener with a count of 1', () => { 59 EventsOnce('a', () => {}) 60 expect(eventListeners['a'][0].maxCallbacks).toBe(1) 61 }) 62 63 it('should return a cancel fn', () => { 64 const cancel = EventsOn('a', () => {}) 65 cancel(); 66 expect(window.WailsInvoke).toBeCalledTimes(1); 67 expect(window.WailsInvoke).toHaveBeenLastCalledWith('EXa'); 68 }) 69 }) 70 71 describe('EventsNotify', () => { 72 it('should inform a listener', () => { 73 const cb = vi.fn() 74 EventsOn('a', cb) 75 EventsNotify(JSON.stringify({name: 'a', data: ["one", "two", "three"]})) 76 expect(cb).toBeCalledTimes(1); 77 expect(cb).toHaveBeenLastCalledWith("one", "two", "three"); 78 expect(window.WailsInvoke).toBeCalledTimes(0); 79 }) 80 }) 81 82 describe('EventsEmit', () => { 83 it('should emit an event', () => { 84 EventsEmit('a', 'one', 'two', 'three') 85 expect(window.WailsInvoke).toBeCalledTimes(1); 86 const calledWith = window.WailsInvoke.calls[0][0]; 87 expect(calledWith.slice(0, 2)).toBe('EE') 88 expect(JSON.parse(calledWith.slice(2))).toStrictEqual({data: ["one", "two", "three"], name: "a"}) 89 }) 90 }) 91 92 describe('EventsOff', () => { 93 beforeEach(() => { 94 EventsOn('a', () => {}) 95 EventsOn('a', () => {}) 96 EventsOn('a', () => {}) 97 EventsOn('b', () => {}) 98 EventsOn('c', () => {}) 99 }) 100 101 it('should cancel all event listeners for a single type', () => { 102 EventsOff('a') 103 expect(eventListeners['a']).toBeUndefined() 104 expect(eventListeners['b']).not.toBeUndefined() 105 expect(eventListeners['c']).not.toBeUndefined() 106 expect(window.WailsInvoke).toBeCalledTimes(1); 107 expect(window.WailsInvoke).toHaveBeenLastCalledWith('EXa'); 108 }) 109 110 it('should cancel all event listeners for multiple types', () => { 111 EventsOff('a', 'b') 112 expect(eventListeners['a']).toBeUndefined() 113 expect(eventListeners['b']).toBeUndefined() 114 expect(eventListeners['c']).not.toBeUndefined() 115 expect(window.WailsInvoke).toBeCalledTimes(2); 116 expect(window.WailsInvoke.calls).toStrictEqual([['EXa'], ['EXb']]); 117 }) 118 }) 119 120 describe('EventsOffAll', () => { 121 it('should cancel all event listeners', () => { 122 EventsOn('a', () => {}) 123 EventsOn('a', () => {}) 124 EventsOn('a', () => {}) 125 EventsOn('b', () => {}) 126 EventsOn('c', () => {}) 127 EventsOffAll() 128 expect(eventListeners).toStrictEqual({}) 129 expect(window.WailsInvoke).toBeCalledTimes(3); 130 expect(window.WailsInvoke.calls).toStrictEqual([['EXa'], ['EXb'], ['EXc']]); 131 }) 132 })