withExperiment.spec.jsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {fireEvent, render, screen} from 'sentry-test/reactTestingLibrary';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import {logExperiment} from 'sentry/utils/analytics';
  4. import withExperiment from 'sentry/utils/withExperiment';
  5. jest.mock('sentry/utils/analytics', () => ({
  6. logExperiment: jest.fn(),
  7. }));
  8. jest.mock('sentry/data/experimentConfig', () => ({
  9. experimentConfig: {
  10. orgExperiment: {
  11. key: 'orgExperiment',
  12. type: 'organization',
  13. parameter: 'exposed',
  14. assignments: [1, 0, -1],
  15. },
  16. userExperiment: {
  17. key: 'userExperiment',
  18. type: 'user',
  19. parameter: 'exposed',
  20. assignments: [1, 0, -1],
  21. },
  22. },
  23. }));
  24. describe('withConfig HoC', function () {
  25. beforeEach(function () {
  26. jest.clearAllMocks();
  27. });
  28. const organization = {
  29. id: 1,
  30. experiments: {orgExperiment: 1},
  31. };
  32. function MyComponent(props) {
  33. return <span>{props.experimentAssignment}</span>;
  34. }
  35. function TriggerComponent(props) {
  36. return <button onClick={props.logExperiment}>click me</button>;
  37. }
  38. it('injects org experiment assignment', function () {
  39. const Container = withExperiment(MyComponent, {experiment: 'orgExperiment'});
  40. render(<Container organization={organization} />);
  41. expect(screen.getByText('1')).toBeInTheDocument();
  42. });
  43. it('injects user experiment assignment', function () {
  44. ConfigStore.set('user', {id: 123, experiments: {userExperiment: 2}});
  45. const Container = withExperiment(MyComponent, {experiment: 'userExperiment'});
  46. render(<Container />);
  47. expect(screen.getByText('2')).toBeInTheDocument();
  48. });
  49. it('logs experiment assignment', function () {
  50. const Container = withExperiment(MyComponent, {experiment: 'orgExperiment'});
  51. render(<Container organization={organization} />);
  52. expect(logExperiment).toHaveBeenCalledWith({key: 'orgExperiment', organization});
  53. });
  54. it('defers logging when injectLogExperiment is true', function () {
  55. const Container = withExperiment(TriggerComponent, {
  56. experiment: 'orgExperiment',
  57. injectLogExperiment: true,
  58. });
  59. render(<Container organization={organization} />);
  60. expect(logExperiment).not.toHaveBeenCalled();
  61. // Call log experiment and verify it was called
  62. fireEvent.click(screen.getByRole('button'));
  63. expect(logExperiment).toHaveBeenCalledWith({key: 'orgExperiment', organization});
  64. });
  65. });