UNPKG

@exadel/esl

Version:

Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components

47 lines (46 loc) 2.19 kB
import '../../actions/external-action'; import { ESLShareActionRegistry } from '../../core/esl-share-action-registry'; import { ESLShareButton } from '../../core/esl-share-button'; describe('ESLShare: "external" action import', () => { test('"external" action was registered when importing', () => { expect(ESLShareActionRegistry.instance.has('external')).toBe(true); }); }); describe('ESLShare: "external" action public API', () => { const originalCreateElement = document.createElement; const mockAnchorElement = { click: jest.fn() }; Object.defineProperty(mockAnchorElement, 'href', { get: () => this._href, set: (value) => this._href = value, }); const mockCreateElement = (tag) => { if (tag === 'a') return mockAnchorElement; return originalCreateElement(tag); }; const externalAction = ESLShareActionRegistry.instance.get('external'); const $button = ESLShareButton.create(); beforeAll(() => { ESLShareButton.register(); document.body.appendChild($button); $button.setAttribute('link', '//host/sharer?title={title}&url={url}&t={t}&u={u}'); $button.setAttribute('share-title', 'Test btn title'); $button.setAttribute('share-url', '/test/url'); }); afterEach(() => { jest.clearAllMocks(); }); afterAll(() => { document.body.innerHTML = ''; jest.restoreAllMocks(); }); test('should be available', () => { expect(externalAction === null || externalAction === void 0 ? void 0 : externalAction.isAvailable).toBe(true); }); test('should simulate click() on anchor to make external jump (also checks link placeholders replacement)', () => { jest.spyOn(document, 'createElement').mockImplementation(mockCreateElement); externalAction === null || externalAction === void 0 ? void 0 : externalAction.share($button); expect(mockAnchorElement.click).toBeCalledTimes(1); expect(mockAnchorElement.href).toBe('//host/sharer?title=Test%20btn%20title&url=http%3A%2F%2Flocalhost%2Ftest%2Furl&t=Test%20btn%20title&u=http%3A%2F%2Flocalhost%2Ftest%2Furl'); }); });