Skip to content
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

Merged
merged 4 commits into from
Apr 8, 2022

Conversation

yen-tt
Copy link
Contributor

@yen-tt yen-tt commented Apr 7, 2022

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 in preview.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):
Screen Shot 2022-04-07 at 4 33 23 PM

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 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.

@yen-tt yen-tt requested a review from a team as a code owner April 7, 2022 20:50
autoCompleteService: AutocompleteService;

constructor() {
this.searchService = generateMockedSearchService();
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

@oshi97 oshi97 left a 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

@yen-tt yen-tt merged commit 1246842 into main Apr 8, 2022
@yen-tt yen-tt deleted the dev/mock-answers-core-only branch April 8, 2022 16:22
yen-tt added a commit that referenced this pull request Sep 28, 2022
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants