testUtils.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import {Fragment} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import GlobalModal from 'sentry/components/globalModal';
  5. import {Organization, Outcome, Project} from 'sentry/types';
  6. import {
  7. RecommendedSdkUpgrade,
  8. SamplingConditionOperator,
  9. SamplingDistribution,
  10. SamplingInnerName,
  11. SamplingInnerOperator,
  12. SamplingRule,
  13. SamplingRuleType,
  14. SamplingSdkVersion,
  15. } from 'sentry/types/sampling';
  16. import {OrganizationContext} from 'sentry/views/organizationContext';
  17. import {RouteContext} from 'sentry/views/routeContext';
  18. import ServerSideSampling from 'sentry/views/settings/project/server-side-sampling';
  19. export const outcomesWithoutClientDiscarded = {
  20. ...TestStubs.OutcomesWithReason(),
  21. groups: TestStubs.OutcomesWithReason().groups.filter(
  22. group => group.by.outcome !== Outcome.CLIENT_DISCARD
  23. ),
  24. };
  25. export const uniformRule: SamplingRule = {
  26. sampleRate: 0.5,
  27. type: SamplingRuleType.TRACE,
  28. active: false,
  29. condition: {
  30. op: SamplingConditionOperator.AND,
  31. inner: [],
  32. },
  33. id: 1,
  34. };
  35. export const specificRule: SamplingRule = {
  36. sampleRate: 0.2,
  37. active: false,
  38. type: SamplingRuleType.TRACE,
  39. condition: {
  40. op: SamplingConditionOperator.AND,
  41. inner: [
  42. {
  43. op: SamplingInnerOperator.GLOB_MATCH,
  44. name: SamplingInnerName.TRACE_RELEASE,
  45. value: ['1.2.2'],
  46. },
  47. ],
  48. },
  49. id: 2,
  50. };
  51. export const mockedProjects = [
  52. TestStubs.Project({
  53. name: 'javascript',
  54. slug: 'javascript',
  55. id: 1,
  56. }),
  57. TestStubs.Project({
  58. name: 'sentry',
  59. slug: 'sentry',
  60. platform: 'python',
  61. id: 2,
  62. }),
  63. TestStubs.Project({
  64. id: 4,
  65. dynamicSampling: {
  66. rules: [
  67. {
  68. sampleRate: 1,
  69. type: 'trace',
  70. active: false,
  71. condition: {
  72. op: 'and',
  73. inner: [],
  74. },
  75. id: 1,
  76. },
  77. ],
  78. },
  79. }),
  80. ];
  81. export const mockedSamplingSdkVersions: SamplingSdkVersion[] = [
  82. {
  83. project: mockedProjects[0].slug,
  84. latestSDKVersion: '1.0.3',
  85. latestSDKName: 'sentry.javascript.react',
  86. isSendingSampleRate: true,
  87. isSendingSource: true,
  88. isSupportedPlatform: true,
  89. },
  90. {
  91. project: mockedProjects[1].slug,
  92. latestSDKVersion: '1.0.2',
  93. latestSDKName: 'sentry.python',
  94. isSendingSampleRate: false,
  95. isSendingSource: false,
  96. isSupportedPlatform: true,
  97. },
  98. {
  99. project: 'java',
  100. latestSDKVersion: '1.0.2',
  101. latestSDKName: 'sentry.java',
  102. isSendingSampleRate: true,
  103. isSendingSource: false,
  104. isSupportedPlatform: true,
  105. },
  106. {
  107. project: 'angular',
  108. latestSDKVersion: '1.0.2',
  109. latestSDKName: 'sentry.javascript.angular',
  110. isSendingSampleRate: false,
  111. isSendingSource: false,
  112. isSupportedPlatform: false,
  113. },
  114. ];
  115. export const recommendedSdkUpgrades: RecommendedSdkUpgrade[] = [
  116. {
  117. project: mockedProjects[1],
  118. latestSDKName: mockedSamplingSdkVersions[1].latestSDKName,
  119. latestSDKVersion: mockedSamplingSdkVersions[1].latestSDKVersion,
  120. },
  121. ];
  122. export const mockedSamplingDistribution: SamplingDistribution = {
  123. projectBreakdown: [
  124. {
  125. project: mockedProjects[0].slug,
  126. projectId: mockedProjects[0].id,
  127. 'count()': 888,
  128. },
  129. {
  130. project: mockedProjects[1].slug,
  131. projectId: mockedProjects[1].id,
  132. 'count()': 100,
  133. },
  134. ],
  135. parentProjectBreakdown: [
  136. {
  137. percentage: 10,
  138. project: 'parent-project',
  139. projectId: 10,
  140. },
  141. ],
  142. sampleSize: 100,
  143. startTimestamp: '2017-08-04T07:52:11Z',
  144. endTimestamp: '2017-08-05T07:52:11Z',
  145. };
  146. export function getMockData({
  147. projects,
  148. access,
  149. }: {access?: string[]; projects?: Project[]} = {}) {
  150. return initializeOrg({
  151. ...initializeOrg(),
  152. organization: {
  153. ...initializeOrg().organization,
  154. features: [
  155. 'server-side-sampling',
  156. 'dynamic-sampling-deprecated',
  157. 'dynamic-sampling',
  158. ],
  159. access: access ?? initializeOrg().organization.access,
  160. projects,
  161. },
  162. projects,
  163. });
  164. }
  165. export function TestComponent({
  166. router,
  167. project,
  168. organization,
  169. withModal,
  170. }: {
  171. organization: Organization;
  172. project: Project;
  173. router?: InjectedRouter;
  174. withModal?: boolean;
  175. }) {
  176. const children = (
  177. <Fragment>
  178. {withModal && <GlobalModal />}
  179. <OrganizationContext.Provider value={organization}>
  180. <ServerSideSampling project={project} />
  181. </OrganizationContext.Provider>
  182. </Fragment>
  183. );
  184. if (router) {
  185. return (
  186. <RouteContext.Provider
  187. value={{
  188. router,
  189. location: router.location,
  190. params: {
  191. orgId: organization.slug,
  192. projectId: project.slug,
  193. },
  194. routes: [],
  195. }}
  196. >
  197. {children}
  198. </RouteContext.Provider>
  199. );
  200. }
  201. return children;
  202. }