|
| 1 | +import React, { ReactNode } from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import If from './mdx/If'; |
| 4 | +import CodeSnippet from '@ably/ui/core/CodeSnippet'; |
| 5 | + |
| 6 | +// Mock the dependencies we need for testing |
| 7 | +jest.mock('./MDXWrapper', () => { |
| 8 | + return { |
| 9 | + __esModule: true, |
| 10 | + default: ({ children, pageContext }: { children: ReactNode; pageContext: any }) => ( |
| 11 | + <div data-testid="mdx-wrapper"> |
| 12 | + {pageContext?.frontmatter?.title && <h1>{pageContext.frontmatter.title}</h1>} |
| 13 | + <div data-testid="mdx-content">{children}</div> |
| 14 | + </div> |
| 15 | + ), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +// Mock the layout context |
| 20 | +jest.mock('src/contexts/layout-context', () => ({ |
| 21 | + useLayoutContext: () => ({ |
| 22 | + activePage: { language: 'javascript' }, |
| 23 | + setLanguage: jest.fn(), |
| 24 | + }), |
| 25 | + LayoutProvider: ({ children }: { children: ReactNode }) => <div data-testid="layout-provider">{children}</div>, |
| 26 | +})); |
| 27 | + |
| 28 | +// We need to mock minimal implementation of other dependencies that CodeSnippet might use |
| 29 | +jest.mock('@ably/ui/core/Icon', () => { |
| 30 | + return { |
| 31 | + __esModule: true, |
| 32 | + default: ({ name, size, additionalCSS, color }: any) => ( |
| 33 | + <span |
| 34 | + data-testid={`icon-${name}`} |
| 35 | + className={`${additionalCSS || ''} ${color || ''}`} |
| 36 | + style={{ width: size, height: size }} |
| 37 | + > |
| 38 | + {name} |
| 39 | + </span> |
| 40 | + ), |
| 41 | + }; |
| 42 | +}); |
| 43 | + |
| 44 | +// Mock Code component used by CodeSnippet |
| 45 | +jest.mock('@ably/ui/core/Code', () => { |
| 46 | + return { |
| 47 | + __esModule: true, |
| 48 | + default: ({ language, snippet }: any) => ( |
| 49 | + <pre data-testid={`code-${language}`}> |
| 50 | + <code>{snippet}</code> |
| 51 | + </pre> |
| 52 | + ), |
| 53 | + }; |
| 54 | +}); |
| 55 | + |
| 56 | +describe('MDX component integration', () => { |
| 57 | + it('renders basic content correctly', () => { |
| 58 | + render( |
| 59 | + <div> |
| 60 | + <h1>Test Heading</h1> |
| 61 | + <p>Test paragraph</p> |
| 62 | + </div>, |
| 63 | + ); |
| 64 | + |
| 65 | + expect(screen.getByText('Test Heading')).toBeInTheDocument(); |
| 66 | + expect(screen.getByText('Test paragraph')).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('conditionally renders content with If component', () => { |
| 70 | + render( |
| 71 | + <div> |
| 72 | + <If lang="javascript">This should be visible</If> |
| 73 | + <If lang="ruby">This should not be visible</If> |
| 74 | + </div>, |
| 75 | + ); |
| 76 | + |
| 77 | + expect(screen.getByText('This should be visible')).toBeInTheDocument(); |
| 78 | + expect(screen.queryByText('This should not be visible')).not.toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('renders code snippets with different languages (JavaScript active)', () => { |
| 82 | + render( |
| 83 | + <div> |
| 84 | + <CodeSnippet> |
| 85 | + <pre> |
| 86 | + <code className="language-javascript"> |
| 87 | + {`var ably = new Ably.Realtime('API_KEY'); |
| 88 | +var channel = ably.channels.get('channel-name'); |
| 89 | + |
| 90 | +// Subscribe to messages on channel |
| 91 | +channel.subscribe('event', function(message) { |
| 92 | + console.log(message.data); |
| 93 | +});`} |
| 94 | + </code> |
| 95 | + </pre> |
| 96 | + <pre> |
| 97 | + <code className="language-swift"> |
| 98 | + {`let realtime = ARTRealtime(key: "API_KEY") |
| 99 | +let channel = realtime.channels.get("channel-name") |
| 100 | + |
| 101 | +// Subscribe to messages on channel |
| 102 | +channel.subscribe("event") { message in |
| 103 | + print(message.data) |
| 104 | +}`} |
| 105 | + </code> |
| 106 | + </pre> |
| 107 | + </CodeSnippet> |
| 108 | + </div>, |
| 109 | + ); |
| 110 | + |
| 111 | + const javascriptElement = screen.queryByTestId('code-javascript'); |
| 112 | + const swiftElement = screen.queryByTestId('code-swift'); |
| 113 | + |
| 114 | + expect(javascriptElement).toBeInTheDocument(); |
| 115 | + expect(swiftElement).not.toBeInTheDocument(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('renders code snippets (TypeScript active)', () => { |
| 119 | + render( |
| 120 | + <div> |
| 121 | + <CodeSnippet lang="typescript"> |
| 122 | + <pre> |
| 123 | + <code className="language-javascript"> |
| 124 | + {`var ably = new Ably.Realtime('API_KEY'); |
| 125 | +var channel = ably.channels.get('channel-name'); |
| 126 | + |
| 127 | +// Subscribe to messages on channel |
| 128 | +channel.subscribe('event', function(message) { |
| 129 | + console.log(message.data); |
| 130 | +});`} |
| 131 | + </code> |
| 132 | + </pre> |
| 133 | + <pre> |
| 134 | + <code className="language-typescript"> |
| 135 | + {`const ably = new Ably.Realtime('API_KEY'); |
| 136 | +const channel = ably.channels.get('channel-name'); |
| 137 | + |
| 138 | +// Subscribe to messages on channel |
| 139 | +channel.subscribe('event', (message: Ably.Types.Message) => { |
| 140 | + console.log(message.data); |
| 141 | +});`} |
| 142 | + </code> |
| 143 | + </pre> |
| 144 | + </CodeSnippet> |
| 145 | + </div>, |
| 146 | + ); |
| 147 | + |
| 148 | + const javascriptElement = screen.queryByTestId('code-javascript'); |
| 149 | + const typescriptElement = screen.queryByTestId('code-typescript'); |
| 150 | + |
| 151 | + expect(javascriptElement).not.toBeInTheDocument(); |
| 152 | + expect(typescriptElement).toBeInTheDocument(); |
| 153 | + }); |
| 154 | +}); |
0 commit comments