feature.spec.jsx 8.2 KB

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