generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Added coverage for experiment on Sidebar
- Loading branch information
Showing
7 changed files
with
159 additions
and
18 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,115 @@ | ||
import React from 'react'; | ||
import { screen } from '@testing-library/react'; | ||
import { render, act } from '../../utils/utils.test'; | ||
import { initialState } from '../../data/slice'; | ||
import { PROMPT_EXPERIMENT_FLAG, PROMPT_EXPERIMENT_KEY } from '../../constants/experiments'; | ||
import { showControlSurvey, showVariationSurvey } from '../../utils/surveyMonkey'; | ||
|
||
import Sidebar from '.'; | ||
|
||
jest.mock('../../utils/surveyMonkey', () => ({ | ||
showControlSurvey: jest.fn(), | ||
showVariationSurvey: jest.fn(), | ||
})); | ||
|
||
const defaultProps = { | ||
courseId: 'some-course-id', | ||
isOpen: true, | ||
setIsOpen: jest.fn(), | ||
unitId: 'some-unit-id', | ||
}; | ||
|
||
const renderSidebar = async (props = {}, sliceState = {}) => { | ||
const componentProps = { | ||
...defaultProps, | ||
...props, | ||
}; | ||
|
||
const initState = { | ||
preloadedState: { | ||
learningAssistant: { | ||
...initialState, | ||
...sliceState, | ||
}, | ||
}, | ||
}; | ||
return act(async () => render( | ||
<Sidebar {...componentProps} />, | ||
initState, | ||
)); | ||
}; | ||
|
||
describe('<Sidebar />', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('when it\'s open', () => { | ||
it('should render normally', () => { | ||
renderSidebar(); | ||
expect(screen.queryByTestId('sidebar')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not render xpert if no disclosureAcknowledged', () => { | ||
renderSidebar(); | ||
expect(screen.queryByTestId('sidebar-xpert')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render xpert if disclosureAcknowledged', () => { | ||
renderSidebar(undefined, { disclosureAcknowledged: true }); | ||
expect(screen.queryByTestId('sidebar-xpert')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('when it\'s not open', () => { | ||
it('should not render', () => { | ||
renderSidebar({ isOpen: false }); | ||
expect(screen.queryByTestId('sidebar')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('prompt experiment', () => { | ||
const defaultState = { | ||
messageList: [{ | ||
role: 'user', | ||
content: 'Testing message 1', | ||
timestamp: +Date.now(), | ||
}, { | ||
role: 'user', | ||
content: 'Testing message 2', | ||
timestamp: +Date.now(), | ||
}], | ||
experiments: { | ||
[PROMPT_EXPERIMENT_FLAG]: { | ||
enabled: true, | ||
variationKey: PROMPT_EXPERIMENT_KEY, | ||
}, | ||
}, | ||
}; | ||
|
||
it('should call showVariationSurvey if experiment is active', async () => { | ||
renderSidebar(undefined, defaultState); | ||
|
||
await act(() => { | ||
screen.queryByTestId('close-button').click(); | ||
}); | ||
|
||
expect(showVariationSurvey).toHaveBeenCalled(); | ||
expect(showControlSurvey).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call showControlSurvey if experiment is not active', async () => { | ||
renderSidebar(undefined, { | ||
...defaultState, | ||
experiments: {}, | ||
}); | ||
|
||
await act(() => { | ||
screen.queryByTestId('close-button').click(); | ||
}); | ||
|
||
expect(showControlSurvey).toHaveBeenCalled(); | ||
expect(showVariationSurvey).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1,3 +1,16 @@ | ||
import 'core-js/stable'; | ||
import 'regenerator-runtime/runtime'; | ||
import '@testing-library/jest-dom'; | ||
|
||
jest.mock( | ||
'@optimizely/react-sdk', | ||
() => { | ||
const originalModule = jest.requireActual('@optimizely/react-sdk'); | ||
return { | ||
__esModule: true, | ||
...originalModule, | ||
useDecision: jest.fn(() => [{ enabled: true, variationKey: 'control' }]), | ||
}; | ||
}, | ||
{ virtual: true }, | ||
); |
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