useExperiment.spec.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {UserFixture} from 'sentry-fixture/user';
  3. import {renderHook} from 'sentry-test/reactTestingLibrary';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import * as useOrganization from 'sentry/utils/useOrganization';
  6. import {useExperiment} from 'getsentry/hooks/useExperiment';
  7. import * as logExperiment from 'getsentry/utils/logExperiment';
  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('useExperiment', function () {
  25. const organization = OrganizationFixture({
  26. id: '1',
  27. experiments: {
  28. // @ts-expect-error: Using mock keys
  29. orgExperiment: 1,
  30. },
  31. });
  32. beforeEach(function () {
  33. jest.clearAllMocks();
  34. jest.spyOn(useOrganization, 'default').mockReturnValue(organization);
  35. jest.spyOn(logExperiment, 'default').mockResolvedValue();
  36. });
  37. it('injects org experiment assignment', function () {
  38. const {result} = renderHook(useExperiment, {
  39. initialProps: 'orgExperiment' as any, // Cast to any because this doesn't exist in the config types
  40. });
  41. expect(result.current).toEqual(
  42. expect.objectContaining({
  43. experimentAssignment: 1,
  44. })
  45. );
  46. });
  47. it('injects user experiment assignment', function () {
  48. ConfigStore.set('user', UserFixture({id: '123', experiments: {userExperiment: 2}}));
  49. const {result} = renderHook(useExperiment, {
  50. initialProps: 'userExperiment' as any,
  51. });
  52. expect(result.current).toEqual(
  53. expect.objectContaining({
  54. experimentAssignment: 2,
  55. })
  56. );
  57. });
  58. it('logs experiment assignment', function () {
  59. const logExperimentSpy = jest.spyOn(logExperiment, 'default').mockResolvedValue();
  60. renderHook(useExperiment, {
  61. initialProps: 'orgExperiment' as any,
  62. });
  63. expect(logExperimentSpy).toHaveBeenCalledWith({key: 'orgExperiment', organization});
  64. });
  65. it('defers logging when logExperimentOnMount is true', function () {
  66. const logExperimentSpy = jest.spyOn(logExperiment, 'default').mockResolvedValue();
  67. const {result} = renderHook(
  68. (args: Parameters<typeof useExperiment>) => useExperiment(args[0], args[1]),
  69. {initialProps: ['orgExperiment' as any, {logExperimentOnMount: false}]}
  70. );
  71. expect(logExperimentSpy).not.toHaveBeenCalled();
  72. result.current.logExperiment();
  73. expect(logExperimentSpy).toHaveBeenCalledWith({key: 'orgExperiment', organization});
  74. });
  75. });