feature.spec.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import Feature from 'sentry/components/acl/feature';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import HookStore from 'sentry/stores/hookStore';
  5. describe('Feature', function () {
  6. const organization = TestStubs.Organization({
  7. features: ['org-foo', 'org-bar', 'bar'],
  8. });
  9. const project = TestStubs.Project({
  10. features: ['project-foo', 'project-bar'],
  11. });
  12. const routerContext = TestStubs.routerContext([
  13. {
  14. organization,
  15. project,
  16. },
  17. ]);
  18. describe('as render prop', function () {
  19. const childrenMock = jest.fn().mockReturnValue(null);
  20. beforeEach(function () {
  21. childrenMock.mockClear();
  22. });
  23. it('has features', function () {
  24. const features = ['org-foo', 'project-foo'];
  25. render(<Feature features={features}>{childrenMock}</Feature>, {
  26. context: routerContext,
  27. });
  28. expect(childrenMock).toHaveBeenCalledWith({
  29. hasFeature: true,
  30. features,
  31. organization,
  32. project,
  33. renderDisabled: false,
  34. });
  35. });
  36. it('has features when requireAll is false', function () {
  37. const features = ['org-foo', 'project-foo', 'apple'];
  38. render(
  39. <Feature features={features} requireAll={false}>
  40. {childrenMock}
  41. </Feature>,
  42. {context: routerContext}
  43. );
  44. expect(childrenMock).toHaveBeenCalledWith({
  45. hasFeature: true,
  46. organization,
  47. project,
  48. features,
  49. renderDisabled: false,
  50. });
  51. });
  52. it('has no features', function () {
  53. render(<Feature features={['org-baz']}>{childrenMock}</Feature>, {
  54. context: routerContext,
  55. });
  56. expect(childrenMock).toHaveBeenCalledWith({
  57. hasFeature: false,
  58. organization,
  59. project,
  60. features: ['org-baz'],
  61. renderDisabled: false,
  62. });
  63. });
  64. it('calls render function when no features', function () {
  65. const noFeatureRenderer = jest.fn(() => null);
  66. render(
  67. <Feature features={['org-baz']} renderDisabled={noFeatureRenderer}>
  68. {childrenMock}
  69. </Feature>,
  70. {context: routerContext}
  71. );
  72. expect(childrenMock).not.toHaveBeenCalled();
  73. expect(noFeatureRenderer).toHaveBeenCalledWith({
  74. hasFeature: false,
  75. children: childrenMock,
  76. organization,
  77. project,
  78. features: ['org-baz'],
  79. });
  80. });
  81. it('can specify org from props', function () {
  82. const customOrg = TestStubs.Organization({features: ['org-bazar']});
  83. render(
  84. <Feature organization={customOrg} features={['org-bazar']}>
  85. {childrenMock}
  86. </Feature>,
  87. {context: routerContext}
  88. );
  89. expect(childrenMock).toHaveBeenCalledWith({
  90. hasFeature: true,
  91. organization: customOrg,
  92. project,
  93. features: ['org-bazar'],
  94. renderDisabled: false,
  95. });
  96. });
  97. it('can specify project from props', function () {
  98. const customProject = TestStubs.Project({features: ['project-baz']});
  99. render(
  100. <Feature project={customProject} features={['project-baz']}>
  101. {childrenMock}
  102. </Feature>,
  103. {context: routerContext}
  104. );
  105. expect(childrenMock).toHaveBeenCalledWith({
  106. hasFeature: true,
  107. organization,
  108. project: customProject,
  109. features: ['project-baz'],
  110. renderDisabled: false,
  111. });
  112. });
  113. it('handles no org/project', function () {
  114. const features = ['org-foo', 'project-foo'];
  115. render(<Feature features={features}>{childrenMock}</Feature>, {
  116. context: routerContext,
  117. });
  118. expect(childrenMock).toHaveBeenCalledWith(
  119. expect.objectContaining({
  120. hasFeature: true,
  121. organization,
  122. project,
  123. features,
  124. renderDisabled: false,
  125. })
  126. );
  127. });
  128. it('handles features prefixed with org/project', function () {
  129. render(<Feature features={['organizations:org-bar']}>{childrenMock}</Feature>, {
  130. context: routerContext,
  131. });
  132. expect(childrenMock).toHaveBeenCalledWith({
  133. hasFeature: true,
  134. organization,
  135. project,
  136. features: ['organizations:org-bar'],
  137. renderDisabled: false,
  138. });
  139. render(<Feature features={['projects:bar']}>{childrenMock}</Feature>, {
  140. context: routerContext,
  141. });
  142. expect(childrenMock).toHaveBeenCalledWith({
  143. hasFeature: false,
  144. organization,
  145. project,
  146. features: ['projects:bar'],
  147. renderDisabled: false,
  148. });
  149. });
  150. it('checks ConfigStore.config.features (e.g. `organizations:create`)', function () {
  151. ConfigStore.config = {
  152. features: new Set(['organizations:create']),
  153. };
  154. render(<Feature features={['organizations:create']}>{childrenMock}</Feature>, {
  155. context: routerContext,
  156. });
  157. expect(childrenMock).toHaveBeenCalledWith({
  158. hasFeature: true,
  159. organization,
  160. project,
  161. features: ['organizations:create'],
  162. renderDisabled: false,
  163. });
  164. });
  165. });
  166. describe('no children', function () {
  167. it('should display renderDisabled with no feature', function () {
  168. render(
  169. <Feature features={['nope']} renderDisabled={() => <span>disabled</span>} />,
  170. {context: routerContext}
  171. );
  172. expect(screen.getByText('disabled')).toBeInTheDocument();
  173. });
  174. it('should display be empty when on', function () {
  175. render(
  176. <Feature features={['org-bar']} renderDisabled={() => <span>disabled</span>} />,
  177. {context: routerContext}
  178. );
  179. expect(screen.queryByText('disabled')).not.toBeInTheDocument();
  180. });
  181. });
  182. describe('as React node', function () {
  183. it('has features', function () {
  184. render(
  185. <Feature features={['org-bar']}>
  186. <div>The Child</div>
  187. </Feature>,
  188. {context: routerContext}
  189. );
  190. expect(screen.getByText('The Child')).toBeInTheDocument();
  191. });
  192. it('has no features', function () {
  193. render(
  194. <Feature features={['org-baz']}>
  195. <div>The Child</div>
  196. </Feature>,
  197. {context: routerContext}
  198. );
  199. expect(screen.queryByText('The Child')).not.toBeInTheDocument();
  200. });
  201. it('renders a default disabled component', function () {
  202. render(
  203. <Feature features={['org-baz']} renderDisabled>
  204. <div>The Child</div>
  205. </Feature>,
  206. {context: routerContext}
  207. );
  208. expect(screen.getByText('This feature is coming soon!')).toBeInTheDocument();
  209. expect(screen.queryByText('The Child')).not.toBeInTheDocument();
  210. });
  211. it('calls renderDisabled function when no features', function () {
  212. const noFeatureRenderer = jest.fn(() => null);
  213. const children = <div>The Child</div>;
  214. render(
  215. <Feature features={['org-baz']} renderDisabled={noFeatureRenderer}>
  216. {children}
  217. </Feature>,
  218. {context: routerContext}
  219. );
  220. expect(screen.queryByText('The Child')).not.toBeInTheDocument();
  221. expect(noFeatureRenderer).toHaveBeenCalledWith({
  222. hasFeature: false,
  223. children,
  224. organization,
  225. project,
  226. features: ['org-baz'],
  227. });
  228. });
  229. });
  230. describe('using HookStore for renderDisabled', function () {
  231. let hookFn;
  232. beforeEach(function () {
  233. hookFn = jest.fn(() => null);
  234. HookStore.hooks['feature-disabled:org-baz'] = [hookFn];
  235. HookStore.hooks['feature-disabled:test-hook'] = [hookFn];
  236. });
  237. afterEach(function () {
  238. delete HookStore.hooks['feature-disabled:org-baz'];
  239. });
  240. it('uses hookName if provided', function () {
  241. const children = <div>The Child</div>;
  242. render(
  243. <Feature features={['org-bazar']} hookName="feature-disabled:test-hook">
  244. {children}
  245. </Feature>,
  246. {context: routerContext}
  247. );
  248. expect(screen.queryByText('The Child')).not.toBeInTheDocument();
  249. expect(hookFn).toHaveBeenCalledWith({
  250. hasFeature: false,
  251. children,
  252. organization,
  253. project,
  254. features: ['org-bazar'],
  255. });
  256. });
  257. });
  258. });