globalSdkUpdateAlert.spec.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import moment from 'moment';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {InnerGlobalSdkUpdateAlert} from 'sentry/components/globalSdkUpdateAlert';
  4. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  5. import {PageFilters, ProjectSdkUpdates} from 'sentry/types';
  6. import {DEFAULT_SNOOZE_PROMPT_DAYS} from 'sentry/utils/promptIsDismissed';
  7. import importedUsePageFilters from 'sentry/utils/usePageFilters';
  8. jest.mock('sentry/utils/usePageFilters');
  9. const usePageFilters = importedUsePageFilters as jest.MockedFunction<
  10. typeof importedUsePageFilters
  11. >;
  12. const makeFilterProps = (
  13. filters: Partial<PageFilters>
  14. ): ReturnType<typeof importedUsePageFilters> => {
  15. return {
  16. isReady: 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. MockApiClient.addMockResponse({
  62. url: '/prompts-activity/',
  63. body: promptResponse,
  64. });
  65. const {rerender} = render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  66. organization: TestStubs.Organization(),
  67. });
  68. expect(
  69. await screen.findByText(/You have outdated SDKs in your projects/)
  70. ).toBeInTheDocument();
  71. usePageFilters.mockImplementation(() => makeFilterProps({projects: [2]}));
  72. // ProjectId no longer matches, so updates should not be shown anymore
  73. rerender(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />);
  74. expect(
  75. screen.queryByText(/You have outdated SDKs in your projects/)
  76. ).not.toBeInTheDocument();
  77. });
  78. it('shows prompt if it has never been dismissed', async () => {
  79. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  80. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  81. const promptResponse = {
  82. dismissed_ts: undefined,
  83. snoozed_ts: undefined,
  84. };
  85. MockApiClient.addMockResponse({
  86. url: '/prompts-activity/',
  87. body: {data: promptResponse},
  88. });
  89. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  90. organization: TestStubs.Organization(),
  91. });
  92. expect(
  93. await screen.findByText(/You have outdated SDKs in your projects/)
  94. ).toBeInTheDocument();
  95. });
  96. it('never shows prompt if it has been dismissed', async () => {
  97. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  98. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  99. const promptResponse = {
  100. dismissed_ts: moment
  101. .utc()
  102. .subtract(DEFAULT_SNOOZE_PROMPT_DAYS - 5, 'days')
  103. .unix(),
  104. snoozed_ts: undefined,
  105. };
  106. MockApiClient.addMockResponse({
  107. url: '/prompts-activity/',
  108. body: {data: promptResponse},
  109. });
  110. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  111. organization: TestStubs.Organization(),
  112. });
  113. await waitFor(() =>
  114. expect(
  115. screen.queryByText(/You have outdated SDKs in your projects/)
  116. ).not.toBeInTheDocument()
  117. );
  118. });
  119. it('shows prompt if snoozed_ts days is longer than threshold', async () => {
  120. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  121. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  122. const promptResponse = {
  123. dismissed_ts: undefined,
  124. snoozed_ts: moment
  125. .utc()
  126. .subtract(DEFAULT_SNOOZE_PROMPT_DAYS + 1, 'days')
  127. .unix(),
  128. };
  129. MockApiClient.addMockResponse({
  130. url: '/prompts-activity/',
  131. body: {data: promptResponse},
  132. });
  133. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  134. organization: TestStubs.Organization(),
  135. });
  136. expect(
  137. await screen.findByText(/You have outdated SDKs in your projects/)
  138. ).toBeInTheDocument();
  139. });
  140. it('shows prompt if snoozed_ts is shorter than threshold', async () => {
  141. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  142. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  143. const promptResponse = {
  144. dismissed_ts: undefined,
  145. snoozed_ts: moment
  146. .utc()
  147. .subtract(DEFAULT_SNOOZE_PROMPT_DAYS - 2, 'days')
  148. .unix(),
  149. };
  150. MockApiClient.addMockResponse({
  151. url: '/prompts-activity/',
  152. body: {data: promptResponse},
  153. });
  154. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  155. organization: TestStubs.Organization(),
  156. });
  157. await waitFor(() =>
  158. expect(
  159. screen.queryByText(/You have outdated SDKs in your projects/)
  160. ).not.toBeInTheDocument()
  161. );
  162. });
  163. it('shows prompt for all projects when project matches ALL_ACCESS_PROJECTS', async () => {
  164. // We intentionally missmatch ALL_ACCESS_PROJECTS with projectId in sdkUpdates
  165. usePageFilters.mockImplementation(() =>
  166. makeFilterProps({projects: [ALL_ACCESS_PROJECTS]})
  167. );
  168. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  169. const promptResponse = {
  170. dismissed_ts: undefined,
  171. snoozed_ts: undefined,
  172. };
  173. MockApiClient.addMockResponse({
  174. url: '/prompts-activity/',
  175. body: promptResponse,
  176. });
  177. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  178. organization: TestStubs.Organization(),
  179. });
  180. expect(
  181. await screen.findByText(/You have outdated SDKs in your projects/)
  182. ).toBeInTheDocument();
  183. });
  184. it('dimisses prompt', async () => {
  185. usePageFilters.mockImplementation(() => makeFilterProps({projects: [0]}));
  186. const sdkUpdates = makeSdkUpdateProps({projectId: String(0)});
  187. const promptResponse = {
  188. dismissed_ts: undefined,
  189. snoozed_ts: undefined,
  190. };
  191. const promptsActivityMock = MockApiClient.addMockResponse({
  192. url: '/prompts-activity/',
  193. body: {data: promptResponse},
  194. });
  195. render(<InnerGlobalSdkUpdateAlert sdkUpdates={sdkUpdates} />, {
  196. organization: TestStubs.Organization(),
  197. });
  198. userEvent.click(await screen.findByText(/Remind me later/));
  199. expect(promptsActivityMock).toHaveBeenCalledWith(
  200. '/prompts-activity/',
  201. expect.objectContaining({
  202. query: expect.objectContaining({
  203. feature: 'sdk_updates',
  204. organization_id: '3',
  205. }),
  206. })
  207. );
  208. expect(
  209. screen.queryByText(/You have outdated SDKs in your projects/)
  210. ).not.toBeInTheDocument();
  211. });
  212. });