globalSdkUpdateAlert.spec.tsx 6.9 KB

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