|
| 1 | +import React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import Sinon from 'sinon'; |
| 6 | +import PreviewPage from '../../../../views/data-browsing-app/preview-page'; |
| 7 | +import vscode from '../../../../views/data-browsing-app/vscode-api'; |
| 8 | +import { PreviewMessageType } from '../../../../views/data-browsing-app/extension-app-message-constants'; |
| 9 | + |
| 10 | +describe('PreviewPage test suite', function () { |
| 11 | + afterEach(function () { |
| 12 | + Sinon.restore(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should render the preview page with toolbar', function () { |
| 16 | + render(<PreviewPage />); |
| 17 | + expect(screen.getByLabelText('Insert Document')).to.exist; |
| 18 | + expect(screen.getByLabelText(/Sort order/)).to.exist; |
| 19 | + expect(screen.getByLabelText(/Items per page/)).to.exist; |
| 20 | + }); |
| 21 | + |
| 22 | + it('should request documents on mount', function () { |
| 23 | + const postMessageStub = Sinon.stub(vscode, 'postMessage'); |
| 24 | + render(<PreviewPage />); |
| 25 | + |
| 26 | + expect(postMessageStub).to.have.been.calledWith({ |
| 27 | + command: PreviewMessageType.getDocuments, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should display loading state initially', function () { |
| 32 | + render(<PreviewPage />); |
| 33 | + expect(screen.getByText('Loading documents...')).to.exist; |
| 34 | + }); |
| 35 | + |
| 36 | + it('should display documents when loaded', async function () { |
| 37 | + render(<PreviewPage />); |
| 38 | + |
| 39 | + // Simulate receiving documents from extension |
| 40 | + window.dispatchEvent( |
| 41 | + new MessageEvent('message', { |
| 42 | + data: { |
| 43 | + command: PreviewMessageType.loadDocuments, |
| 44 | + documents: [ |
| 45 | + { _id: '1', name: 'Test Document 1' }, |
| 46 | + { _id: '2', name: 'Test Document 2' }, |
| 47 | + ], |
| 48 | + totalCount: 2, |
| 49 | + }, |
| 50 | + }), |
| 51 | + ); |
| 52 | + |
| 53 | + await waitFor(() => { |
| 54 | + expect(screen.queryByText('Loading documents...')).to.not.exist; |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should send refresh request when refresh button is clicked', async function () { |
| 59 | + const postMessageStub = Sinon.stub(vscode, 'postMessage'); |
| 60 | + render(<PreviewPage />); |
| 61 | + |
| 62 | + // Wait for initial load to complete |
| 63 | + window.dispatchEvent( |
| 64 | + new MessageEvent('message', { |
| 65 | + data: { |
| 66 | + command: PreviewMessageType.loadDocuments, |
| 67 | + documents: [], |
| 68 | + totalCount: 0, |
| 69 | + }, |
| 70 | + }), |
| 71 | + ); |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + expect(screen.queryByText('Loading documents...')).to.not.exist; |
| 75 | + }); |
| 76 | + |
| 77 | + const refreshButton = screen.getByTitle('Refresh'); |
| 78 | + await userEvent.click(refreshButton); |
| 79 | + |
| 80 | + expect(postMessageStub).to.have.been.calledWith({ |
| 81 | + command: PreviewMessageType.refreshDocuments, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should send sort request when sort option changes', async function () { |
| 86 | + const postMessageStub = Sinon.stub(vscode, 'postMessage'); |
| 87 | + render(<PreviewPage />); |
| 88 | + |
| 89 | + // Wait for initial load |
| 90 | + window.dispatchEvent( |
| 91 | + new MessageEvent('message', { |
| 92 | + data: { |
| 93 | + command: PreviewMessageType.loadDocuments, |
| 94 | + documents: [], |
| 95 | + totalCount: 0, |
| 96 | + }, |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + await waitFor(() => { |
| 101 | + expect(screen.queryByText('Loading documents...')).to.not.exist; |
| 102 | + }); |
| 103 | + |
| 104 | + const sortSelect = screen.getByLabelText(/Sort order/); |
| 105 | + await userEvent.click(sortSelect); |
| 106 | + |
| 107 | + const ascOption = screen.getByText('Ascending'); |
| 108 | + await userEvent.click(ascOption); |
| 109 | + |
| 110 | + expect(postMessageStub).to.have.been.calledWith({ |
| 111 | + command: PreviewMessageType.sortDocuments, |
| 112 | + sort: 'asc', |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should display "No documents to display" when there are no documents', async function () { |
| 117 | + render(<PreviewPage />); |
| 118 | + |
| 119 | + window.dispatchEvent( |
| 120 | + new MessageEvent('message', { |
| 121 | + data: { |
| 122 | + command: PreviewMessageType.loadDocuments, |
| 123 | + documents: [], |
| 124 | + totalCount: 0, |
| 125 | + }, |
| 126 | + }), |
| 127 | + ); |
| 128 | + |
| 129 | + await waitFor(() => { |
| 130 | + expect(screen.getByText('No documents to display')).to.exist; |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle pagination correctly', async function () { |
| 135 | + const documents = Array.from({ length: 25 }, (_, i) => ({ |
| 136 | + _id: `${i + 1}`, |
| 137 | + name: `Document ${i + 1}`, |
| 138 | + })); |
| 139 | + |
| 140 | + render(<PreviewPage />); |
| 141 | + |
| 142 | + window.dispatchEvent( |
| 143 | + new MessageEvent('message', { |
| 144 | + data: { |
| 145 | + command: PreviewMessageType.loadDocuments, |
| 146 | + documents, |
| 147 | + totalCount: 25, |
| 148 | + }, |
| 149 | + }), |
| 150 | + ); |
| 151 | + |
| 152 | + await waitFor(() => { |
| 153 | + expect(screen.queryByText('Loading documents...')).to.not.exist; |
| 154 | + }); |
| 155 | + |
| 156 | + // Should show first 10 documents by default |
| 157 | + expect(screen.getByText('1-10 of 25')).to.exist; |
| 158 | + |
| 159 | + // Click next page |
| 160 | + const nextButton = screen.getByLabelText('Next page'); |
| 161 | + await userEvent.click(nextButton); |
| 162 | + |
| 163 | + expect(screen.getByText('11-20 of 25')).to.exist; |
| 164 | + }); |
| 165 | +}); |
0 commit comments