with-split-tests.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import type { Meta } from '@storybook/react'
  2. import _ from 'lodash'
  3. import { SplitTestContext } from '@/shared/context/split-test-context'
  4. export const defaultSplitTestsArgTypes = {
  5. // to be able to use this utility, you need to add the argTypes for each split test in this object
  6. // Check the original implementation for an example: https://github.com/overleaf/internal/pull/17809
  7. 'editor-redesign': {
  8. description: 'Enable the new editor redesign',
  9. control: {
  10. type: 'select' as const,
  11. },
  12. options: ['enabled'],
  13. },
  14. uniaccessphase1: {
  15. description: 'Enable CIAM designs',
  16. control: { type: 'select' as const },
  17. options: ['default', 'enabled'],
  18. },
  19. }
  20. export const withSplitTests = <ArgTypes = typeof defaultSplitTestsArgTypes,>(
  21. story: Meta,
  22. splitTests: (keyof ArgTypes)[] = [],
  23. /** @deprecated For demo purposes only. Add actual split tests in defaultSplitTestsArgTypes */
  24. _splitTestsArgTypes?: ArgTypes
  25. ): Meta => {
  26. const splitTestsArgTypes = _splitTestsArgTypes ?? defaultSplitTestsArgTypes
  27. return {
  28. ...story,
  29. argTypes: { ...story.argTypes, ..._.pick(splitTestsArgTypes, splitTests) },
  30. decorators: [
  31. (Story, { args }) => {
  32. const splitTestVariants = _.pick(args, splitTests)
  33. const value = { splitTestVariants, splitTestInfo: {} }
  34. return (
  35. <SplitTestContext.Provider value={value}>
  36. <Story />
  37. </SplitTestContext.Provider>
  38. )
  39. },
  40. ...(story.decorators
  41. ? Array.isArray(story.decorators)
  42. ? story.decorators
  43. : [story.decorators]
  44. : []),
  45. ],
  46. }
  47. }