UserSettingsHelper.test.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/Features/Project/UserSettingsHelper.mjs'
  4. describe('UserSettingsHelper', function () {
  5. beforeEach(async function (ctx) {
  6. ctx.SplitTestHandler = {
  7. promises: { getAssignment: sinon.stub() },
  8. }
  9. vi.doMock(
  10. '../../../../app/src/Features/SplitTests/SplitTestHandler.mjs',
  11. () => ({
  12. default: ctx.SplitTestHandler,
  13. })
  14. )
  15. ctx.req = { query: {} }
  16. ctx.res = {}
  17. ctx.UserSettingsHelper = (await import(modulePath)).default
  18. })
  19. describe('for user with overall theme set to value', function () {
  20. beforeEach(async function (ctx) {
  21. const user = {
  22. ace: {
  23. overallTheme: 'light',
  24. },
  25. signUpDate: new Date('2022-01-01'),
  26. }
  27. ctx.settings = await ctx.UserSettingsHelper.buildUserSettings(
  28. ctx.req,
  29. ctx.res,
  30. user
  31. )
  32. })
  33. it('should return the user settings with the correct overall theme', function (ctx) {
  34. expect(ctx.settings.overallTheme).toBe('light')
  35. })
  36. it('should not check split test', function (ctx) {
  37. expect(ctx.SplitTestHandler.promises.getAssignment).not.toHaveBeenCalled
  38. })
  39. })
  40. describe('for user with no overall theme set', function () {
  41. describe('for new users in treatment group', function () {
  42. beforeEach(async function (ctx) {
  43. const user = {
  44. ace: {},
  45. signUpDate: new Date('2026-02-16T00:00:00Z'),
  46. }
  47. ctx.SplitTestHandler.promises.getAssignment
  48. .withArgs(ctx.req, ctx.res, 'new-user-system-overall-theme')
  49. .resolves({
  50. variant: 'system',
  51. })
  52. ctx.settings = await ctx.UserSettingsHelper.buildUserSettings(
  53. ctx.req,
  54. ctx.res,
  55. user
  56. )
  57. })
  58. it('should default to system theme', function (ctx) {
  59. expect(ctx.settings.overallTheme).toBe('system')
  60. })
  61. it('should check split test', function (ctx) {
  62. expect(ctx.SplitTestHandler.promises.getAssignment).toHaveBeenCalled
  63. })
  64. })
  65. describe('for new users in control group', function () {
  66. beforeEach(async function (ctx) {
  67. const user = {
  68. ace: {},
  69. signUpDate: new Date('2026-02-16T00:00:00Z'),
  70. }
  71. ctx.SplitTestHandler.promises.getAssignment
  72. .withArgs(ctx.req, ctx.res, 'new-user-system-overall-theme')
  73. .resolves({
  74. variant: 'default',
  75. })
  76. ctx.settings = await ctx.UserSettingsHelper.buildUserSettings(
  77. ctx.req,
  78. ctx.res,
  79. user
  80. )
  81. })
  82. it('should default to dark theme', function (ctx) {
  83. expect(ctx.settings.overallTheme).toBe('')
  84. })
  85. it('should check split test', function (ctx) {
  86. expect(ctx.SplitTestHandler.promises.getAssignment).toHaveBeenCalled
  87. })
  88. })
  89. describe('for old users in control group', function () {
  90. beforeEach(async function (ctx) {
  91. const user = {
  92. ace: {},
  93. signUpDate: new Date('2025-02-15T00:00:00Z'),
  94. }
  95. ctx.SplitTestHandler.promises.getAssignment
  96. .withArgs(ctx.req, ctx.res, 'new-user-system-overall-theme')
  97. .resolves({
  98. variant: 'default',
  99. })
  100. ctx.settings = await ctx.UserSettingsHelper.buildUserSettings(
  101. ctx.req,
  102. ctx.res,
  103. user
  104. )
  105. })
  106. it('should default to dark theme', function (ctx) {
  107. expect(ctx.settings.overallTheme).toBe('')
  108. })
  109. it('should not check split test', function (ctx) {
  110. expect(ctx.SplitTestHandler.promises.getAssignment).not.toHaveBeenCalled
  111. })
  112. })
  113. describe('for old users in treatment group', function () {
  114. beforeEach(async function (ctx) {
  115. const user = {
  116. ace: {},
  117. signUpDate: new Date('2025-02-15T00:00:00Z'),
  118. }
  119. ctx.SplitTestHandler.promises.getAssignment
  120. .withArgs(ctx.req, ctx.res, 'new-user-system-overall-theme')
  121. .resolves({
  122. variant: 'system',
  123. })
  124. ctx.settings = await ctx.UserSettingsHelper.buildUserSettings(
  125. ctx.req,
  126. ctx.res,
  127. user
  128. )
  129. })
  130. it('should default to dark theme', function (ctx) {
  131. expect(ctx.settings.overallTheme).toBe('')
  132. })
  133. it('should not check split test', function (ctx) {
  134. expect(ctx.SplitTestHandler.promises.getAssignment).not.toHaveBeenCalled
  135. })
  136. })
  137. })
  138. })