generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Optimizely refactor #57
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3de26cf
fix: Removing useDecision() to get the experiment states from Optimizely
rijuma 1b28cf5
fix: Optimizely refactor
rijuma 7cf1004
fix: Swapped active for enabled for optimizely prop
rijuma 7ef94c0
fix: Small refactor on useDecision hook
rijuma 24bdda8
fix: Typo in src/data/optimizely.js comment
rijuma 0286b05
fix: Typo in src/mocks/optimizely/index.jsx
rijuma 9d9504e
chore: Added extra comments on the useDecision() usage
rijuma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,195 @@ | ||
import React from 'react'; | ||
import { | ||
screen, act, fireEvent, waitFor, | ||
} from '@testing-library/react'; | ||
import { useDecision } from '@optimizely/react-sdk'; | ||
import { render as renderComponent } from '../../utils/utils.test'; | ||
import { initialState } from '../../data/slice'; | ||
import { OPTIMIZELY_PROMPT_EXPERIMENT_VARIATION_KEYS } from '../../data/optimizely'; | ||
import { | ||
acknowledgeDisclosure, | ||
addChatMessage, | ||
getChatResponse, | ||
updateCurrentMessage, | ||
} from '../../data/thunks'; | ||
|
||
import MessageForm from '.'; | ||
|
||
jest.mock('../../utils/surveyMonkey', () => ({ | ||
showControlSurvey: jest.fn(), | ||
showVariationSurvey: jest.fn(), | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/analytics', () => ({ | ||
sendTrackEvent: jest.fn(), | ||
})); | ||
|
||
const mockedAuthenticatedUser = { userId: 123 }; | ||
jest.mock('@edx/frontend-platform/auth', () => ({ | ||
getAuthenticatedUser: () => mockedAuthenticatedUser, | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/analytics', () => ({ | ||
sendTrackEvent: jest.fn(), | ||
})); | ||
|
||
const mockDispatch = jest.fn(); | ||
jest.mock('react-redux', () => ({ | ||
...jest.requireActual('react-redux'), | ||
useDispatch: () => mockDispatch, | ||
})); | ||
|
||
jest.mock('@optimizely/react-sdk', () => ({ | ||
useDecision: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../data/thunks', () => ({ | ||
acknowledgeDisclosure: jest.fn(), | ||
addChatMessage: jest.fn(), | ||
getChatResponse: jest.fn(), | ||
updateCurrentMessage: jest.fn(), | ||
})); | ||
|
||
const defaultProps = { | ||
courseId: 'some-course-id', | ||
isOpen: true, | ||
setIsOpen: jest.fn(), | ||
unitId: 'some-unit-id', | ||
}; | ||
|
||
const render = async (props = {}, sliceState = {}) => { | ||
const componentProps = { | ||
...defaultProps, | ||
...props, | ||
}; | ||
|
||
const initState = { | ||
preloadedState: { | ||
learningAssistant: { | ||
...initialState, | ||
...sliceState, | ||
}, | ||
}, | ||
}; | ||
return act(async () => renderComponent( | ||
<MessageForm {...componentProps} />, | ||
initState, | ||
)); | ||
}; | ||
|
||
describe('<MessageForm />', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
useDecision.mockReturnValue([]); | ||
}); | ||
|
||
describe('when rendered', () => { | ||
it('should focus if shouldAutofocus is enabled', () => { | ||
const currentMessage = 'How much wood'; | ||
const sliceState = { | ||
apiIsLoading: false, | ||
currentMessage, | ||
apiError: false, | ||
}; | ||
|
||
render({ shouldAutofocus: true }, sliceState); | ||
|
||
waitFor(() => { | ||
expect(screen.getByDisplayValue(currentMessage)).toHaveFocus(); | ||
}); | ||
|
||
expect(screen.queryByTestId('message-form')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should dispatch updateCurrentMessage() when updating the form control', () => { | ||
const currentMessage = 'How much wood'; | ||
const updatedMessage = 'How much wood coud a woodchuck chuck'; | ||
const sliceState = { | ||
apiIsLoading: false, | ||
currentMessage, | ||
apiError: false, | ||
}; | ||
|
||
render(undefined, sliceState); | ||
|
||
act(() => { | ||
const input = screen.getByDisplayValue(currentMessage); | ||
fireEvent.change(input, { target: { value: updatedMessage } }); | ||
}); | ||
|
||
expect(updateCurrentMessage).toHaveBeenCalledWith(updatedMessage); | ||
expect(mockDispatch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should dispatch message on submit as expected', () => { | ||
const currentMessage = 'How much wood could a woodchuck chuck if a woodchuck could chuck wood?'; | ||
const sliceState = { | ||
apiIsLoading: false, | ||
currentMessage, | ||
apiError: false, | ||
}; | ||
|
||
render(undefined, sliceState); | ||
|
||
act(() => { | ||
screen.queryByTestId('message-form-submit').click(); | ||
}); | ||
|
||
expect(acknowledgeDisclosure).toHaveBeenCalledWith(true); | ||
expect(addChatMessage).toHaveBeenCalledWith('user', currentMessage, defaultProps.courseId, undefined); | ||
expect(getChatResponse).toHaveBeenCalledWith(defaultProps.courseId, defaultProps.unitId, undefined); | ||
expect(mockDispatch).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should not dispatch on submit if there\'s no message', () => { | ||
render(); | ||
|
||
act(() => { | ||
screen.queryByTestId('message-form-submit').click(); | ||
}); | ||
|
||
expect(acknowledgeDisclosure).not.toHaveBeenCalled(); | ||
expect(addChatMessage).not.toHaveBeenCalled(); | ||
expect(getChatResponse).not.toHaveBeenCalled(); | ||
expect(mockDispatch).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('prmpt experiment', () => { | ||
beforeEach(() => { | ||
useDecision.mockReturnValue([{ | ||
enabled: true, | ||
variationKey: OPTIMIZELY_PROMPT_EXPERIMENT_VARIATION_KEYS.UPDATED_PROMPT, | ||
}]); | ||
}); | ||
|
||
it('should include experiment data on submit', () => { | ||
const currentMessage = 'How much wood could a woodchuck chuck if a woodchuck could chuck wood?'; | ||
const sliceState = { | ||
apiIsLoading: false, | ||
currentMessage, | ||
apiError: false, | ||
}; | ||
|
||
render(undefined, sliceState); | ||
|
||
act(() => { | ||
screen.queryByTestId('message-form-submit').click(); | ||
}); | ||
|
||
expect(acknowledgeDisclosure).toHaveBeenCalledWith(true); | ||
expect(addChatMessage).toHaveBeenCalledWith( | ||
'user', | ||
currentMessage, | ||
defaultProps.courseId, | ||
OPTIMIZELY_PROMPT_EXPERIMENT_VARIATION_KEYS.UPDATED_PROMPT, | ||
); | ||
expect(getChatResponse).toHaveBeenCalledWith( | ||
defaultProps.courseId, | ||
defaultProps.unitId, | ||
OPTIMIZELY_PROMPT_EXPERIMENT_VARIATION_KEYS.UPDATED_PROMPT, | ||
); | ||
expect(mockDispatch).toHaveBeenCalledTimes(3); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why
autoUpdate
istrue
here? I believe the default isfalse
. Reading the documentation, I don't exactly understand why we'd want to set this totrue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the
id
override have the nameoverrideUserId
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you send the
userId
via theOptimizelyProvider
, which you do via theExperimentsProvider
component, you shouldn't need to send a user ID override, because that user ID will already be available to Optimizely throughout the component tree. Is there a reason we need to override the ID here? I have the same question forsrc/components/Sidebar/index.jsx
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understood that having the autoUpdate: true would react to changes on the experiment immediately while the user is in the experiment. I think it's a good thing to have in cases where we detect a bug and want to remove the experiment. I thought it was an useful thing to use to be able to react in real time.
Indeed. Good catch. I've made a small refactor on the
useDecision()
hook into its ownusePromptExperimentDecision()
hook to avoid repetition.I was worried if for some reason the user changed after the initialization. Since the
getAuthenticatedUser()
is a getter, the provider might not update if this happens. The override would have the updateduserId
when the call is made, so I thought it was more accurate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I don't think those cases are likely to occur in the experiment, but I don't see a good reason not have these options set this way because, as you said, it's safer. I think my main concern is it makes it a little more difficult to understand. The user override, for example, feels like it's serving to perform an override, but the user ID is exactly the same. But that's why I asked the question, and I appreciate you explaining their function! I don't have any concerns with leaving them in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing. I saw the usage in some examples in Optimizely's documentation and saw no reason to exclude it. I've added a short inline comment on each prop to explain the usage.