globalSdkUpdateAlert.spec.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import moment from 'moment';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import {InnerGlobalSdkUpdateAlert} from 'sentry/components/globalSdkUpdateAlert';
  5. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  6. import {PageFilters, ProjectSdkUpdates} from 'sentry/types';
  7. import {DEFAULT_SNOOZE_PROMPT_DAYS} from 'sentry/utils/promptIsDismissed';
  8. import importedUsePageFilters from 'sentry/utils/usePageFilters';
  9. jest.mock('sentry/utils/usePageFilters');
  10. const usePageFilters = jest.mocked(importedUsePageFilters);
  11. const makeFilterProps = (
  12. filters: Partial<PageFilters>
  13. ): ReturnType<typeof importedUsePageFilters> => {
  14. return {
  15. isReady: true,
  16. shouldPersist: true,
  17. desyncedFilters: new Set(),
  18. pinnedFilters: new Set(),
  19. selection: {
  20. projects: [1],
  21. environments: ['prod'],
  22. datetime: {start: new Date(), end: new Date(), period: '14d', utc: true},
  23. ...filters,
  24. },
  25. };
  26. };
  27. const makeSdkUpdateProps = (
  28. sdkUpdateProps: Partial<ProjectSdkUpdates>
  29. ): ProjectSdkUpdates[] => {
  30. return [
  31. {
  32. projectId: String(1),
  33. sdkName: 'sentry-javascript',
  34. sdkVersion: '1.0.0.',
  35. suggestions: [
  36. {
  37. enables: [],
  38. newSdkVersion: '1.1.0',
  39. sdkName: 'sentry-javascript',
  40. type: 'updateSdk',
  41. },
  42. ],
  43. ...sdkUpdateProps,
  44. },
  45. ];
  46. };
  47. describe('GlobalSDKUpdateAlert', () => {
  48. beforeEach(() => {
  49. jest.clearAllMocks();
  50. MockApiClient.clearMockResponses();
  51. usePageFilters.mockClear();
  52. });
  53. it('does not shows prompt if projects do not match', async () => {
  54. // We have matching projectId, so updates should be show
  55. usePageFilters.mockImplementation(() => makeFilterProps({projects: [1]}));
  56. const sdkUpdates = makeSdkUpdateProps({projectId: String(1)});
  57. const promptResponse = {
  58. dismissed_ts: undefined,
  59. snoozed_ts: undefined,
  60. };
  61. const organization = OrganizationFixture();
  62. MockApiClient.addMockResponse({
  63. url: `/organizations/${organization.slug}/prompts-activity/`,
  64. body: promptResponse,
  65. });
  66. const {rerender} = render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  67. organization,
  68. });
  69. expect(
  70. await screen.findByText(/You have outdated SDKs in your projects/)
  71. ).toBeInTheDocument();
  72. usePageFilters.mockImplementation(() => makeFilterProps({projects: [2]}));
  73. // ProjectId no longer matches, so updates should not be shown anymore
  74. rerender(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />);
  75. expect(
  76. screen.queryByText(/You have outdated SDKs in your projects/)
  77. ).not.toBeInTheDocument();
  78. });
  79. it('shows prompt if it has never been dismissed', async () => {
  80. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  81. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  82. const promptResponse = {
  83. dismissed_ts: undefined,
  84. snoozed_ts: undefined,
  85. };
  86. const organization = OrganizationFixture();
  87. MockApiClient.addMockResponse({
  88. url: `/organizations/${organization.slug}/prompts-activity/`,
  89. body: {data: promptResponse},
  90. });
  91. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  92. organization: OrganizationFixture(),
  93. });
  94. expect(
  95. await screen.findByText(/You have outdated SDKs in your projects/)
  96. ).toBeInTheDocument();
  97. });
  98. it('never shows prompt if it has been dismissed', async () => {
  99. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  100. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  101. const promptResponse = {
  102. dismissed_ts: moment
  103. .utc()
  104. .subtract(DEFAULT_SNOOZE_PROMPT_DAYS - 5, 'days')
  105. .unix(),
  106. snoozed_ts: undefined,
  107. };
  108. const organization = OrganizationFixture();
  109. MockApiClient.addMockResponse({
  110. url: `/organizations/${organization.slug}/prompts-activity/`,
  111. body: {data: promptResponse},
  112. });
  113. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  114. organization,
  115. });
  116. await waitFor(() =>
  117. expect(
  118. screen.queryByText(/You have outdated SDKs in your projects/)
  119. ).not.toBeInTheDocument()
  120. );
  121. });
  122. it('shows prompt if snoozed_ts days is longer than threshold', async () => {
  123. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  124. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  125. const promptResponse = {
  126. dismissed_ts: undefined,
  127. snoozed_ts: moment
  128. .utc()
  129. .subtract(DEFAULT_SNOOZE_PROMPT_DAYS + 1, 'days')
  130. .unix(),
  131. };
  132. const organization = OrganizationFixture();
  133. MockApiClient.addMockResponse({
  134. url: `/organizations/${organization.slug}/prompts-activity/`,
  135. body: {data: promptResponse},
  136. });
  137. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  138. organization,
  139. });
  140. expect(
  141. await screen.findByText(/You have outdated SDKs in your projects/)
  142. ).toBeInTheDocument();
  143. });
  144. it('shows prompt if snoozed_ts is shorter than threshold', async () => {
  145. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  146. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  147. const promptResponse = {
  148. dismissed_ts: undefined,
  149. snoozed_ts: moment
  150. .utc()
  151. .subtract(DEFAULT_SNOOZE_PROMPT_DAYS - 2, 'days')
  152. .unix(),
  153. };
  154. const organization = OrganizationFixture();
  155. MockApiClient.addMockResponse({
  156. url: `/organizations/${organization.slug}/prompts-activity/`,
  157. body: {data: promptResponse},
  158. });
  159. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  160. organization,
  161. });
  162. await waitFor(() =>
  163. expect(
  164. screen.queryByText(/You have outdated SDKs in your projects/)
  165. ).not.toBeInTheDocument()
  166. );
  167. });
  168. it('shows prompt for all projects when project matches ALL_ACCESS_PROJECTS', async () => {
  169. // We intentionally missmatch ALL_ACCESS_PROJECTS with projectId in sdkUpdates
  170. usePageFilters.mockImplementation(() =>
  171. makeFilterProps({projects: [ALL_ACCESS_PROJECTS]})
  172. );
  173. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  174. const promptResponse = {
  175. dismissed_ts: undefined,
  176. snoozed_ts: undefined,
  177. };
  178. const organization = OrganizationFixture();
  179. MockApiClient.addMockResponse({
  180. url: `/organizations/${organization.slug}/prompts-activity/`,
  181. body: promptResponse,
  182. });
  183. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  184. organization,
  185. });
  186. expect(
  187. await screen.findByText(/You have outdated SDKs in your projects/)
  188. ).toBeInTheDocument();
  189. });
  190. it('dimisses prompt', async () => {
  191. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  192. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  193. const promptResponse = {
  194. dismissed_ts: undefined,
  195. snoozed_ts: undefined,
  196. };
  197. const organization = OrganizationFixture();
  198. MockApiClient.addMockResponse({
  199. url: `/organizations/${organization.slug}/prompts-activity/`,
  200. body: {data: promptResponse},
  201. });
  202. const promptsActivityMock = MockApiClient.addMockResponse({
  203. url: `/organizations/${organization.slug}/prompts-activity/`,
  204. method: 'PUT',
  205. });
  206. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  207. organization,
  208. });
  209. await userEvent.click(await screen.findByRole('button', {name: 'Remind me later'}));
  210. expect(promptsActivityMock).toHaveBeenCalledWith(
  211. `/organizations/${organization.slug}/prompts-activity/`,
  212. expect.objectContaining({
  213. data: expect.objectContaining({
  214. feature: 'sdk_updates',
  215. organization_id: '3',
  216. }),
  217. })
  218. );
  219. expect(
  220. screen.queryByText(/You have outdated SDKs in your projects/)
  221. ).not.toBeInTheDocument();
  222. });
  223. });