|
| 1 | +jest.dontMock('./PluginManager'); |
| 2 | +jest.dontMock('../services/DependencyStore'); |
| 3 | + |
| 4 | +import { ElementWithMeta } from '../types'; |
| 5 | +import { ElementRegistrar } from '../services/ElementRegistrar'; |
| 6 | +import { getMockedInstance } from '@testFixtures/mocked'; |
| 7 | +import { PluginManager } from './PluginManager'; |
| 8 | +import { DependencyStore } from '../services/DependencyStore'; |
| 9 | +import { TranslationService } from '../services/TranslationService'; |
| 10 | +import { Messages } from './Messages'; |
| 11 | + |
| 12 | +describe('PluginManager', () => { |
| 13 | + let pluginManager: PluginManager; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + pluginManager = new DependencyStore().pluginManager; |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('Can be created', () => { |
| 24 | + expect(pluginManager).toBeInstanceOf(PluginManager); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('take screenshot methods', () => { |
| 28 | + const data = { |
| 29 | + key: 'test_key', |
| 30 | + translations: { en: 'English!' }, |
| 31 | + }; |
| 32 | + |
| 33 | + let resolve; |
| 34 | + const highlightMock = jest.fn(); |
| 35 | + const unHighlightMock = jest.fn(); |
| 36 | + let listenCallback; |
| 37 | + const cancelMock = jest.fn(); |
| 38 | + const revertMock = jest.fn(); |
| 39 | + |
| 40 | + beforeEach(async () => { |
| 41 | + getMockedInstance(TranslationService).changeTranslations = jest.fn( |
| 42 | + () => |
| 43 | + new Promise((r) => { |
| 44 | + resolve = r; |
| 45 | + }) |
| 46 | + ); |
| 47 | + |
| 48 | + getMockedInstance(ElementRegistrar).findAllByKey = jest.fn(() => { |
| 49 | + const element = document.createElement( |
| 50 | + 'span' |
| 51 | + ) as any as ElementWithMeta; |
| 52 | + element._tolgee = { |
| 53 | + highlight: highlightMock, |
| 54 | + unhighlight: unHighlightMock, |
| 55 | + } as any; |
| 56 | + |
| 57 | + return [element]; |
| 58 | + }); |
| 59 | + |
| 60 | + (getMockedInstance(Messages) as any).send = jest.fn(); |
| 61 | + |
| 62 | + (getMockedInstance(Messages) as any).listen = jest.fn((_, callback) => { |
| 63 | + listenCallback = callback; |
| 64 | + return cancelMock; |
| 65 | + }); |
| 66 | + |
| 67 | + pluginManager.takeScreenshot(data); |
| 68 | + resolve(revertMock); |
| 69 | + }); |
| 70 | + |
| 71 | + test('calls change translation', () => { |
| 72 | + expect( |
| 73 | + getMockedInstance(TranslationService).changeTranslations |
| 74 | + ).toHaveBeenCalledWith(data); |
| 75 | + }); |
| 76 | + |
| 77 | + test('sends the message to take screenshots', () => { |
| 78 | + expect((getMockedInstance(Messages) as any).send).toHaveBeenCalledTimes( |
| 79 | + 1 |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + test('highlights all', () => { |
| 84 | + expect( |
| 85 | + getMockedInstance(ElementRegistrar).findAllByKey |
| 86 | + ).toHaveBeenCalledWith(data.key); |
| 87 | + expect( |
| 88 | + getMockedInstance(ElementRegistrar).findAllByKey |
| 89 | + ).toHaveBeenCalledTimes(1); |
| 90 | + expect(highlightMock).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + test('unhighlights all after', () => { |
| 94 | + listenCallback(); |
| 95 | + expect(unHighlightMock).toHaveBeenCalledTimes(1); |
| 96 | + }); |
| 97 | + |
| 98 | + test('reverts the translation change', () => { |
| 99 | + listenCallback(); |
| 100 | + expect(revertMock).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | + |
| 103 | + test('cancels the listening', () => { |
| 104 | + listenCallback(); |
| 105 | + expect(cancelMock).toHaveBeenCalledTimes(1); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments