-
Notifications
You must be signed in to change notification settings - Fork 9
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
use mocked AnswersCore and actual ReduxStateManager in Headless instance #103
Conversation
autoCompleteService: AutocompleteService; | ||
|
||
constructor() { | ||
this.searchService = generateMockedSearchService(); |
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 was wondering if we would want some way to specify these on a per story basis?
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.
Sure! I added a storybook decorator for AnswersCore so you are able to specify specific mock for each story. I updated the description on how you would do that
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.
the decorator seems cool! thanks for looking into it
…nce (#103) Update generateMockedHeadless only mock AnswersCore, and use actual ReduxStateManager instance internally. In jest, this can be done with jest.spyOn. However, storybook doesn't support jest functions globally by default. I attempted to use [jest-mock](https://github.com/facebook/jest/tree/main/packages/jest-mock) and tried setting `ModuleMocker`/`fn()`/`spyOn()` to window.jest in `preview.js` to mock provideCore/AnswersCore, as some have suggested this approach ([1](https://stackoverflow.com/questions/64349461/make-jest-globally-available-in-storybook))([2](https://github.com/storybookjs/builder-vite/issues/242)). However this results in an error in storybook and I couldn't figure a way around it (worked fine in jest tests): <img width="689" alt="Screen Shot 2022-04-07 at 4 33 23 PM" src="https://user-images.githubusercontent.com/36055303/162291157-d718136c-f74c-40c7-a5a5-5f341a9d5ad3.png"> I decided to use the [recommended approach](https://storybook.js.org/docs/react/writing-stories/build-pages-with-storybook#mocking-imports) from Storybook's doc by making an import alias in storybook's webpack config. I attempted to make an alias of `@yext/answers-core` to mock `provideCore()` but this caused a lot of export issues in node_modules from answers-headless-react and answers-headless and I was unable to re-export things from answers-core to resolve these issues. Instead of making an alias import for the whole core library, I end up making an import alias for the `AnswersCore` class instead. Since jest functions can't be inside `generateMockedHeadless` or Storybook would crash, `moduleNameMapper` is added to jest config in package.json to make the same import alias for AnswersCore. added Storybook decorator for AnswersCore. With that, user are able to specify different AnswersCore's mocked services on a per-story basis. Example: ``` export const Primary = () => <SomeComponent/> }; export const Secondary = () => <SomeComponent/> }; Primary.parameters = { answersCoreServices: { searchService: generateMockedSearchService() } }; Secondary.parameters = { answersCoreServices: { searchService: generateADifferentMockedSearchService() } } ``` updated displayName of `DisplayableHierarchicalFacets` due to duplicate key error that came up in storybook. J=SLAP-2036 TEST=manual ran `npm run storybook` and see that the component stories loaded successfully. When click on SpellCheck link in SpellCheck primary story, see that a console log from the mocked AnswersCore appear and no API network request was made. modified SpellCheck jest tests to use `generateMockedHeadless`. See that the test worked as expected with console log from the mocked AnswersCore. construct two SpellCheck stories each with different mocked searchService for its AnswersCore instance. Added log in the different mocked searchService. See that the log from the expected mocked searchService appears when click on the spellcheck link in each story.
Update generateMockedHeadless only mock AnswersCore, and use actual ReduxStateManager instance internally.
In jest, this can be done with jest.spyOn. However, storybook doesn't support jest functions globally by default. I attempted to use jest-mock and tried setting
ModuleMocker
/fn()
/spyOn()
to window.jest inpreview.js
to mock provideCore/AnswersCore, as some have suggested this approach (1)(2). However this results in an error in storybook and I couldn't figure a way around it (worked fine in jest tests):I decided to use the recommended approach from Storybook's doc by making an import alias in storybook's webpack config. I attempted to make an alias of
@yext/answers-core
to mockprovideCore()
but this caused a lot of export issues in node_modules from answers-headless-react and answers-headless and I was unable to re-export things from answers-core to resolve these issues. Instead of making an alias import for the whole core library, I end up making an import alias for theAnswersCore
class instead.Since jest functions can't be inside
generateMockedHeadless
or Storybook would crash,moduleNameMapper
is added to jest config in package.json to make the same import alias for AnswersCore.added Storybook decorator for AnswersCore. With that, user are able to specify different AnswersCore's mocked services on a per-story basis. Example:
updated displayName of
DisplayableHierarchicalFacets
due to duplicate key error that came up in storybook.J=SLAP-2036
TEST=manual
ran
npm run storybook
and see that the component stories loaded successfully. When click on SpellCheck link in SpellCheck primary story, see that a console log from the mocked AnswersCore appear and no API network request was made.modified SpellCheck jest tests to use
generateMockedHeadless
. See that the test worked as expected with console log from the mocked AnswersCore.construct two SpellCheck stories each with different mocked searchService for its AnswersCore instance. Added log in the different mocked searchService. See that the log from the expected mocked searchService appears when click on the spellcheck link in each story.