globalSdkUpdateAlert.spec.tsx 7.4 KB

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