@times-components/ts-components 1.129.5 → 1.129.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ import React, { useEffect, FC } from 'react';
2
+
3
+ type PalinPollsProps = {
4
+ source: string;
5
+ };
6
+
7
+ export const PalinPolls: FC<PalinPollsProps> = ({ source }) => {
8
+ if (!source) {
9
+ return null;
10
+ }
11
+
12
+ useEffect(() => {
13
+ const appendPalinPoll = () => {
14
+ const decodedSource = atob(source);
15
+
16
+ const src = new DOMParser()
17
+ .parseFromString(decodedSource, 'text/html')
18
+ .head.getElementsByTagName('script')[0]
19
+ .getAttribute('src');
20
+
21
+ const pollParent = document.getElementById('poll-parent');
22
+
23
+ if (pollParent && src) {
24
+ let poll = document.createElement('script');
25
+
26
+ poll.setAttribute('src', src);
27
+ pollParent.appendChild(poll);
28
+ }
29
+ };
30
+
31
+ setTimeout(appendPalinPoll, 0);
32
+ }, []);
33
+
34
+ return <div id="poll-parent" data-testid="poll-parent" />;
35
+ };
@@ -0,0 +1,59 @@
1
+ import React from 'react';
2
+ import { render, screen, waitFor } from '@testing-library/react';
3
+ import { PalinPolls } from '../PalinPolls';
4
+ import '@testing-library/jest-dom';
5
+
6
+ describe('PalinPolls component', () => {
7
+ beforeEach(() => {
8
+ jest.clearAllMocks();
9
+ document.body.innerHTML = ''; // Clean up DOM
10
+ });
11
+
12
+ it('should return null if source is empty', () => {
13
+ const { container } = render(<PalinPolls source="" />);
14
+ expect(container.firstChild).toBeNull();
15
+ });
16
+
17
+ it('should render the poll-parent div when source is provided', () => {
18
+ const mockHtml = btoa(
19
+ `<script src="https://example.com/script.js"></script>`
20
+ );
21
+ render(<PalinPolls source={mockHtml} />);
22
+ expect(screen.getByTestId('poll-parent')).toBeInTheDocument();
23
+ });
24
+
25
+ it('should append a script tag with correct src to #poll-parent', async () => {
26
+ const scriptSrc = 'https://example.com/poll.js';
27
+ const encoded = btoa(`<script src="${scriptSrc}"></script>`);
28
+
29
+ render(<PalinPolls source={encoded} />);
30
+
31
+ await waitFor(() => {
32
+ const parent = document.getElementById('poll-parent');
33
+ expect(parent).not.toBeNull();
34
+
35
+ const scripts = parent
36
+ ? Array.from(parent.getElementsByTagName('script'))
37
+ : [];
38
+
39
+ // Look for a script with the correct src
40
+ const targetScript = scripts.find(
41
+ script => script.getAttribute('src') === scriptSrc
42
+ );
43
+ expect(targetScript).toBeDefined();
44
+ });
45
+ });
46
+
47
+ /* it('should not append a script if script tag is missing', async () => {
48
+ const badEncoded = btoa(`<div>No script tag</div>`);
49
+ render(<PalinPolls source={badEncoded} />);
50
+
51
+ await waitFor(() => {
52
+ const parent = document.getElementById('poll-parent');
53
+ expect(parent).not.toBeNull();
54
+
55
+ const scripts = parent ? parent.getElementsByTagName('script') : [];
56
+ expect(scripts.length).toBe(0);
57
+ });
58
+ }); */
59
+ });
package/src/index.ts CHANGED
@@ -128,6 +128,9 @@ export {
128
128
  export { CtaButton } from './components/cta-button/CtaButton';
129
129
  export { SocialMediaEmbed } from './components/social-embed/SocialMediaEmbed';
130
130
 
131
+ // Palin Polls
132
+ export { PalinPolls } from './components/palin-polls/PalinPolls';
133
+
131
134
  // Contexts
132
135
  export {
133
136
  useSocialEmbedsContext,