-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add Consent component unit tests
- Loading branch information
1 parent
1a72343
commit ae97b90
Showing
2 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React from 'react'; | ||
import { fireEvent, render, waitFor } from '@testing-library/react'; | ||
import Consent from './Consent'; | ||
import { useConsent } from '../../API' | ||
import { saveAs } from 'file-saver'; | ||
|
||
global.Blob = jest.fn().mockImplementation((content, options) => ({ | ||
content, | ||
options | ||
})); | ||
|
||
global.URL.createObjectURL = jest.fn(); | ||
|
||
jest.mock('file-saver', () => ({ | ||
saveAs: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../API', () => ({ | ||
createConsent: jest.fn(), | ||
useConsent: jest.fn(), | ||
})); | ||
|
||
const mockExperiment = { | ||
slug: 'test-experiment', | ||
loading_text: 'Loading...', | ||
}; | ||
|
||
describe('Consent', () => { | ||
it('renders loading state correctly', () => { | ||
useConsent.mockReturnValue([null, true]); // Mock loading state | ||
const { getByText } = render(<Consent experiment={{ slug: 'test-experiment', loading_text: 'Loading...' }} />); | ||
expect(getByText('Loading...')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders consent text when not loading', () => { | ||
useConsent.mockReturnValue([null, false]); | ||
const { getByText } = render(<Consent text="<p>Consent Text</p>" experiment={mockExperiment} />); | ||
expect(getByText('Consent Text')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onNext when Agree button is clicked', async () => { | ||
useConsent.mockReturnValue([null, false]); | ||
const onNext = jest.fn(); | ||
const { getByText } = render(<Consent onNext={onNext} confirm="Agree" experiment={mockExperiment} />); | ||
fireEvent.click(getByText('Agree')); | ||
|
||
await waitFor(() => expect(onNext).toHaveBeenCalled()); | ||
}); | ||
|
||
it('triggers download when Download button is clicked', async () => { | ||
useConsent.mockReturnValue([null, false]); | ||
const { getByTestId } = render(<Consent text="<p>Consent Text</p>" experiment={mockExperiment} />); | ||
fireEvent.click(getByTestId('download-button')); | ||
|
||
await waitFor(() => expect(saveAs).toHaveBeenCalled()); | ||
}); | ||
|
||
it('auto advances if consent is already given', () => { | ||
useConsent.mockReturnValue([true, false]); | ||
const onNext = jest.fn(); | ||
render(<Consent onNext={onNext} urlQueryString="?participant_id=123" experiment={mockExperiment} />); | ||
expect(onNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calculates style for consent text correctly', () => { | ||
useConsent.mockReturnValue([null, false]); | ||
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 }); | ||
const { getByTestId } = render(<Consent text="<p>Consent Text</p>" experiment={mockExperiment} />); | ||
const consentText = getByTestId('consent-text'); | ||
expect(consentText.style.height).toBe('500px'); | ||
}); | ||
|
||
|
||
|
||
}); |