helpers.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. import { PluginSignatureStatus, PluginSignatureType, PluginType } from '@grafana/data';
  2. import { config } from '@grafana/runtime';
  3. import { getLocalPluginMock, getRemotePluginMock, getCatalogPluginMock } from './__mocks__';
  4. import {
  5. mapToCatalogPlugin,
  6. mapRemoteToCatalog,
  7. mapLocalToCatalog,
  8. mergeLocalAndRemote,
  9. mergeLocalsAndRemotes,
  10. sortPlugins,
  11. Sorters,
  12. isLocalPluginVisible,
  13. isRemotePluginVisible,
  14. } from './helpers';
  15. import { RemotePlugin, LocalPlugin } from './types';
  16. describe('Plugins/Helpers', () => {
  17. let remotePlugin: RemotePlugin;
  18. let localPlugin: LocalPlugin;
  19. beforeEach(() => {
  20. remotePlugin = getRemotePluginMock();
  21. localPlugin = getLocalPluginMock();
  22. });
  23. describe('mergeLocalsAndRemotes()', () => {
  24. const localPlugins = [
  25. getLocalPluginMock({ id: 'plugin-1' }),
  26. getLocalPluginMock({ id: 'plugin-2' }),
  27. getLocalPluginMock({ id: 'plugin-3' }), // only on local
  28. ];
  29. const remotePlugins = [
  30. getRemotePluginMock({ slug: 'plugin-1' }),
  31. getRemotePluginMock({ slug: 'plugin-2' }),
  32. getRemotePluginMock({ slug: 'plugin-4' }), // only on remote
  33. ];
  34. test('adds all available plugins only once', () => {
  35. const merged = mergeLocalsAndRemotes(localPlugins, remotePlugins);
  36. const mergedIds = merged.map(({ id }) => id);
  37. expect(merged.length).toBe(4);
  38. expect(mergedIds).toContain('plugin-1');
  39. expect(mergedIds).toContain('plugin-2');
  40. expect(mergedIds).toContain('plugin-3');
  41. expect(mergedIds).toContain('plugin-4');
  42. });
  43. test('merges all plugins with their counterpart (if available)', () => {
  44. const merged = mergeLocalsAndRemotes(localPlugins, remotePlugins);
  45. const findMerged = (mergedId: string) => merged.find(({ id }) => id === mergedId);
  46. // Both local & remote counterparts
  47. expect(findMerged('plugin-1')).toEqual(
  48. mergeLocalAndRemote(getLocalPluginMock({ id: 'plugin-1' }), getRemotePluginMock({ slug: 'plugin-1' }))
  49. );
  50. expect(findMerged('plugin-2')).toEqual(
  51. mergeLocalAndRemote(getLocalPluginMock({ id: 'plugin-2' }), getRemotePluginMock({ slug: 'plugin-2' }))
  52. );
  53. // Only local
  54. expect(findMerged('plugin-3')).toEqual(mergeLocalAndRemote(getLocalPluginMock({ id: 'plugin-3' })));
  55. // Only remote
  56. expect(findMerged('plugin-4')).toEqual(mergeLocalAndRemote(undefined, getRemotePluginMock({ slug: 'plugin-4' })));
  57. });
  58. });
  59. describe('mergeLocalAndRemote()', () => {
  60. test('merges using mapRemoteToCatalog() if there is only a remote version', () => {
  61. expect(mergeLocalAndRemote(undefined, remotePlugin)).toEqual(mapRemoteToCatalog(remotePlugin));
  62. });
  63. test('merges using mapLocalToCatalog() if there is only a local version', () => {
  64. expect(mergeLocalAndRemote(localPlugin)).toEqual(mapLocalToCatalog(localPlugin));
  65. });
  66. test('merges using mapToCatalogPlugin() if there is both a remote and a local version', () => {
  67. expect(mergeLocalAndRemote(localPlugin, remotePlugin)).toEqual(mapToCatalogPlugin(localPlugin, remotePlugin));
  68. });
  69. });
  70. describe('mapRemoteToCatalog()', () => {
  71. test('maps the remote response (GCOM /api/plugins/<id>) to PluginCatalog', () => {
  72. expect(mapRemoteToCatalog(remotePlugin)).toEqual({
  73. description: 'Zabbix plugin for Grafana',
  74. downloads: 33645089,
  75. hasUpdate: false,
  76. id: 'alexanderzobnin-zabbix-app',
  77. info: {
  78. logos: {
  79. large: 'https://grafana.com/api/plugins/alexanderzobnin-zabbix-app/versions/4.1.5/logos/large',
  80. small: 'https://grafana.com/api/plugins/alexanderzobnin-zabbix-app/versions/4.1.5/logos/small',
  81. },
  82. },
  83. error: undefined,
  84. isCore: false,
  85. isDev: false,
  86. isDisabled: false,
  87. isEnterprise: false,
  88. isInstalled: false,
  89. isPublished: true,
  90. name: 'Zabbix',
  91. orgName: 'Alexander Zobnin',
  92. popularity: 0.2111,
  93. publishedAt: '2016-04-06T20:23:41.000Z',
  94. signature: 'valid',
  95. type: 'app',
  96. updatedAt: '2021-05-18T14:53:01.000Z',
  97. });
  98. });
  99. test('adds the correct signature enum', () => {
  100. const pluginWithoutSignature = { ...remotePlugin, signatureType: '', versionSignatureType: '' } as RemotePlugin;
  101. // With only "signatureType" -> valid
  102. const pluginWithSignature1 = { ...remotePlugin, signatureType: PluginSignatureType.commercial } as RemotePlugin;
  103. // With only "versionSignatureType" -> valid
  104. const pluginWithSignature2 = { ...remotePlugin, versionSignatureType: PluginSignatureType.core } as RemotePlugin;
  105. expect(mapRemoteToCatalog(pluginWithoutSignature).signature).toBe(PluginSignatureStatus.missing);
  106. expect(mapRemoteToCatalog(pluginWithSignature1).signature).toBe(PluginSignatureStatus.valid);
  107. expect(mapRemoteToCatalog(pluginWithSignature2).signature).toBe(PluginSignatureStatus.valid);
  108. });
  109. test('adds an "isEnterprise" field', () => {
  110. const enterprisePlugin = { ...remotePlugin, status: 'enterprise' } as RemotePlugin;
  111. const notEnterprisePlugin = { ...remotePlugin, status: 'unknown' } as RemotePlugin;
  112. expect(mapRemoteToCatalog(enterprisePlugin).isEnterprise).toBe(true);
  113. expect(mapRemoteToCatalog(notEnterprisePlugin).isEnterprise).toBe(false);
  114. });
  115. test('adds an "isCore" field', () => {
  116. const corePlugin = { ...remotePlugin, internal: true } as RemotePlugin;
  117. const notCorePlugin = { ...remotePlugin, internal: false } as RemotePlugin;
  118. expect(mapRemoteToCatalog(corePlugin).isCore).toBe(true);
  119. expect(mapRemoteToCatalog(notCorePlugin).isCore).toBe(false);
  120. });
  121. });
  122. describe('mapLocalToCatalog()', () => {
  123. test('maps local response to PluginCatalog', () => {
  124. expect(mapLocalToCatalog(localPlugin)).toEqual({
  125. description: 'Zabbix plugin for Grafana',
  126. downloads: 0,
  127. id: 'alexanderzobnin-zabbix-app',
  128. info: {
  129. logos: {
  130. large: 'public/plugins/alexanderzobnin-zabbix-app/img/icn-zabbix-app.svg',
  131. small: 'public/plugins/alexanderzobnin-zabbix-app/img/icn-zabbix-app.svg',
  132. },
  133. },
  134. error: undefined,
  135. hasUpdate: false,
  136. isCore: false,
  137. isDev: false,
  138. isDisabled: false,
  139. isEnterprise: false,
  140. isInstalled: true,
  141. isPublished: false,
  142. name: 'Zabbix',
  143. orgName: 'Alexander Zobnin',
  144. popularity: 0,
  145. publishedAt: '',
  146. signature: 'valid',
  147. signatureOrg: 'Alexander Zobnin',
  148. signatureType: 'community',
  149. type: 'app',
  150. updatedAt: '2021-08-25',
  151. installedVersion: '4.2.2',
  152. });
  153. });
  154. test('isCore if signature is internal', () => {
  155. const pluginWithoutInternalSignature = { ...localPlugin };
  156. const pluginWithInternalSignature = { ...localPlugin, signature: 'internal' } as LocalPlugin;
  157. expect(mapLocalToCatalog(pluginWithoutInternalSignature).isCore).toBe(false);
  158. expect(mapLocalToCatalog(pluginWithInternalSignature).isCore).toBe(true);
  159. });
  160. test('isDev if local.dev', () => {
  161. const pluginWithoutDev = { ...localPlugin, dev: false };
  162. const pluginWithDev = { ...localPlugin, dev: true };
  163. expect(mapLocalToCatalog(pluginWithoutDev).isDev).toBe(false);
  164. expect(mapLocalToCatalog(pluginWithDev).isDev).toBe(true);
  165. });
  166. });
  167. describe('mapToCatalogPlugin()', () => {
  168. test('merges local and remote plugin data correctly', () => {
  169. expect(mapToCatalogPlugin(localPlugin, remotePlugin)).toEqual({
  170. description: 'Zabbix plugin for Grafana',
  171. downloads: 33645089,
  172. hasUpdate: false,
  173. id: 'alexanderzobnin-zabbix-app',
  174. info: {
  175. logos: {
  176. small: 'https://grafana.com/api/plugins/alexanderzobnin-zabbix-app/versions/4.1.5/logos/small',
  177. large: 'https://grafana.com/api/plugins/alexanderzobnin-zabbix-app/versions/4.1.5/logos/large',
  178. },
  179. },
  180. error: undefined,
  181. isCore: false,
  182. isDev: false,
  183. isDisabled: false,
  184. isEnterprise: false,
  185. isInstalled: true,
  186. isPublished: true,
  187. name: 'Zabbix',
  188. orgName: 'Alexander Zobnin',
  189. popularity: 0.2111,
  190. publishedAt: '2016-04-06T20:23:41.000Z',
  191. signature: 'valid',
  192. signatureOrg: 'Alexander Zobnin',
  193. signatureType: 'community',
  194. type: 'app',
  195. updatedAt: '2021-05-18T14:53:01.000Z',
  196. installedVersion: '4.2.2',
  197. });
  198. });
  199. test('`.description` - prefers the local', () => {
  200. // Local & Remote
  201. expect(
  202. mapToCatalogPlugin(
  203. { ...localPlugin, info: { ...localPlugin.info, description: 'Local description' } },
  204. { ...remotePlugin, description: 'Remote description' }
  205. )
  206. ).toMatchObject({ description: 'Local description' });
  207. // Remote only
  208. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, description: 'Remote description' })).toMatchObject({
  209. description: 'Remote description',
  210. });
  211. // Local only
  212. expect(
  213. mapToCatalogPlugin({ ...localPlugin, info: { ...localPlugin.info, description: 'Local description' } })
  214. ).toMatchObject({ description: 'Local description' });
  215. // No local or remote
  216. expect(mapToCatalogPlugin()).toMatchObject({ description: '' });
  217. });
  218. test('`.hasUpdate` - prefers the local', () => {
  219. // Local only
  220. expect(mapToCatalogPlugin({ ...localPlugin })).toMatchObject({ hasUpdate: false });
  221. expect(mapToCatalogPlugin({ ...localPlugin, hasUpdate: true })).toMatchObject({ hasUpdate: true });
  222. // No local or remote
  223. expect(mapToCatalogPlugin()).toMatchObject({ hasUpdate: false });
  224. });
  225. test('`.downloads` - relies on the remote', () => {
  226. // Local & Remote
  227. expect(mapToCatalogPlugin(localPlugin, { ...remotePlugin, downloads: 99 })).toMatchObject({ downloads: 99 });
  228. // Remote only
  229. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, downloads: 99 })).toMatchObject({ downloads: 99 });
  230. // Local only
  231. expect(mapToCatalogPlugin(localPlugin)).toMatchObject({ downloads: 0 });
  232. // No local or remote
  233. expect(mapToCatalogPlugin()).toMatchObject({ downloads: 0 });
  234. });
  235. test('`.isCore` - prefers the remote', () => {
  236. // Local & Remote
  237. expect(mapToCatalogPlugin(localPlugin, { ...remotePlugin, internal: true })).toMatchObject({ isCore: true });
  238. // Remote only
  239. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, internal: true })).toMatchObject({ isCore: true });
  240. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, internal: false })).toMatchObject({ isCore: false });
  241. // Local only
  242. expect(mapToCatalogPlugin({ ...localPlugin, signature: PluginSignatureStatus.internal })).toMatchObject({
  243. isCore: true,
  244. });
  245. expect(mapToCatalogPlugin({ ...localPlugin, signature: PluginSignatureStatus.valid })).toMatchObject({
  246. isCore: false,
  247. });
  248. // No local or remote
  249. expect(mapToCatalogPlugin()).toMatchObject({ isCore: false });
  250. });
  251. test('`.isDev` - prefers the local', () => {
  252. // Local & Remote
  253. expect(mapToCatalogPlugin({ ...localPlugin, dev: true }, remotePlugin)).toMatchObject({ isDev: true });
  254. // Remote only
  255. expect(mapToCatalogPlugin(undefined, remotePlugin)).toMatchObject({ isDev: false });
  256. // Local only
  257. expect(mapToCatalogPlugin({ ...localPlugin, dev: true }, undefined)).toMatchObject({ isDev: true });
  258. expect(mapToCatalogPlugin({ ...localPlugin, dev: undefined }, undefined)).toMatchObject({ isDev: false });
  259. // No local or remote
  260. expect(mapToCatalogPlugin()).toMatchObject({ isDev: false });
  261. });
  262. test('`.isEnterprise` - prefers the remote', () => {
  263. // Local & Remote
  264. expect(mapToCatalogPlugin(localPlugin, { ...remotePlugin, status: 'enterprise' })).toMatchObject({
  265. isEnterprise: true,
  266. });
  267. expect(mapToCatalogPlugin(localPlugin, { ...remotePlugin, status: 'unknown' })).toMatchObject({
  268. isEnterprise: false,
  269. });
  270. // Remote only
  271. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, status: 'enterprise' })).toMatchObject({
  272. isEnterprise: true,
  273. });
  274. // Local only
  275. expect(mapToCatalogPlugin(localPlugin)).toMatchObject({ isEnterprise: false });
  276. // No local or remote
  277. expect(mapToCatalogPlugin()).toMatchObject({ isEnterprise: false });
  278. });
  279. test('`.isInstalled` - prefers the local', () => {
  280. // Local & Remote
  281. expect(mapToCatalogPlugin(localPlugin, remotePlugin)).toMatchObject({ isInstalled: true });
  282. // Remote only
  283. expect(mapToCatalogPlugin(undefined, remotePlugin)).toMatchObject({ isInstalled: false });
  284. // Local only
  285. expect(mapToCatalogPlugin(localPlugin, undefined)).toMatchObject({ isInstalled: true });
  286. // No local or remote
  287. expect(mapToCatalogPlugin()).toMatchObject({ isInstalled: false });
  288. });
  289. test('`.name` - prefers the remote', () => {
  290. // Local & Remote
  291. expect(
  292. mapToCatalogPlugin({ ...localPlugin, name: 'Local name' }, { ...remotePlugin, name: 'Remote name' })
  293. ).toMatchObject({ name: 'Remote name' });
  294. // Remote only
  295. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, name: 'Remote name' })).toMatchObject({
  296. name: 'Remote name',
  297. });
  298. // Local only
  299. expect(mapToCatalogPlugin({ ...localPlugin, name: 'Local name' })).toMatchObject({ name: 'Local name' });
  300. // No local or remote
  301. expect(mapToCatalogPlugin()).toMatchObject({ name: '' });
  302. });
  303. test('`.orgName` - prefers the remote', () => {
  304. // Local & Remote
  305. expect(mapToCatalogPlugin(localPlugin, { ...remotePlugin, orgName: 'Remote org' })).toMatchObject({
  306. orgName: 'Remote org',
  307. });
  308. // Remote only
  309. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, orgName: 'Remote org' })).toMatchObject({
  310. orgName: 'Remote org',
  311. });
  312. // Local only
  313. expect(mapToCatalogPlugin(localPlugin)).toMatchObject({ orgName: 'Alexander Zobnin' });
  314. // No local or remote
  315. expect(mapToCatalogPlugin()).toMatchObject({ orgName: '' });
  316. });
  317. test('`.popularity` - prefers the remote', () => {
  318. // Local & Remote
  319. expect(mapToCatalogPlugin(localPlugin, { ...remotePlugin, popularity: 10 })).toMatchObject({ popularity: 10 });
  320. // Remote only
  321. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, popularity: 10 })).toMatchObject({ popularity: 10 });
  322. // Local only
  323. expect(mapToCatalogPlugin(localPlugin)).toMatchObject({ popularity: 0 });
  324. // No local or remote
  325. expect(mapToCatalogPlugin()).toMatchObject({ popularity: 0 });
  326. });
  327. test('`.publishedAt` - prefers the remote', () => {
  328. // Local & Remote
  329. expect(mapToCatalogPlugin(localPlugin, { ...remotePlugin, createdAt: '2020-01-01' })).toMatchObject({
  330. publishedAt: '2020-01-01',
  331. });
  332. // Remote only
  333. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, createdAt: '2020-01-01' })).toMatchObject({
  334. publishedAt: '2020-01-01',
  335. });
  336. // Local only
  337. expect(mapToCatalogPlugin(localPlugin)).toMatchObject({ publishedAt: '' });
  338. // No local or remote
  339. expect(mapToCatalogPlugin()).toMatchObject({ publishedAt: '' });
  340. });
  341. test('`.type` - prefers the local', () => {
  342. // Local & Remote
  343. expect(
  344. mapToCatalogPlugin(
  345. { ...localPlugin, type: PluginType.app },
  346. { ...remotePlugin, typeCode: PluginType.datasource }
  347. )
  348. ).toMatchObject({
  349. type: PluginType.app,
  350. });
  351. // Remote only
  352. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, typeCode: PluginType.datasource })).toMatchObject({
  353. type: PluginType.datasource,
  354. });
  355. // Local only
  356. expect(mapToCatalogPlugin({ ...localPlugin, type: PluginType.app })).toMatchObject({
  357. type: PluginType.app,
  358. });
  359. // No local or remote
  360. expect(mapToCatalogPlugin()).toMatchObject({ type: undefined });
  361. });
  362. test('`.signature` - prefers the local', () => {
  363. // Local & Remote
  364. expect(
  365. mapToCatalogPlugin(
  366. { ...localPlugin, signature: PluginSignatureStatus.valid },
  367. { ...remotePlugin, signatureType: '', versionSignatureType: '' }
  368. )
  369. ).toMatchObject({
  370. signature: PluginSignatureStatus.valid,
  371. });
  372. expect(
  373. mapToCatalogPlugin(
  374. { ...localPlugin, signature: PluginSignatureStatus.missing },
  375. {
  376. ...remotePlugin,
  377. signatureType: PluginSignatureType.grafana,
  378. versionSignatureType: PluginSignatureType.grafana,
  379. }
  380. )
  381. ).toMatchObject({
  382. signature: PluginSignatureStatus.missing,
  383. });
  384. // Remote only
  385. expect(
  386. mapToCatalogPlugin(undefined, { ...remotePlugin, signatureType: PluginSignatureType.grafana })
  387. ).toMatchObject({
  388. signature: PluginSignatureStatus.valid,
  389. });
  390. expect(
  391. mapToCatalogPlugin(undefined, { ...remotePlugin, versionSignatureType: PluginSignatureType.grafana })
  392. ).toMatchObject({
  393. signature: PluginSignatureStatus.valid,
  394. });
  395. expect(
  396. mapToCatalogPlugin(undefined, { ...remotePlugin, signatureType: '', versionSignatureType: '' })
  397. ).toMatchObject({
  398. signature: PluginSignatureStatus.missing,
  399. });
  400. // Local only
  401. expect(mapToCatalogPlugin({ ...localPlugin, signature: PluginSignatureStatus.valid })).toMatchObject({
  402. signature: PluginSignatureStatus.valid,
  403. });
  404. // No local or remote
  405. expect(mapToCatalogPlugin()).toMatchObject({ signature: PluginSignatureStatus.missing });
  406. });
  407. test('`.signatureOrg` - prefers the local', () => {
  408. // Local & Remote
  409. expect(
  410. mapToCatalogPlugin(
  411. { ...localPlugin, signatureOrg: 'Local Org' },
  412. { ...remotePlugin, versionSignedByOrgName: 'Remote Org' }
  413. )
  414. ).toMatchObject({
  415. signatureOrg: 'Local Org',
  416. });
  417. // Remote only
  418. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, versionSignedByOrgName: 'Remote Org' })).toMatchObject({
  419. signatureOrg: 'Remote Org',
  420. });
  421. // Local only
  422. expect(mapToCatalogPlugin({ ...localPlugin, signatureOrg: 'Local Org' })).toMatchObject({
  423. signatureOrg: 'Local Org',
  424. });
  425. // No local or remote
  426. expect(mapToCatalogPlugin()).toMatchObject({ signatureOrg: undefined });
  427. });
  428. test('`.signatureType` - prefers the local', () => {
  429. // Local & Remote
  430. expect(
  431. mapToCatalogPlugin(
  432. { ...localPlugin, signatureType: PluginSignatureType.core },
  433. { ...remotePlugin, signatureType: PluginSignatureType.grafana }
  434. )
  435. ).toMatchObject({
  436. signatureType: PluginSignatureType.core,
  437. });
  438. // Remote only
  439. expect(
  440. mapToCatalogPlugin(undefined, {
  441. ...remotePlugin,
  442. versionSignatureType: PluginSignatureType.core,
  443. signatureType: PluginSignatureType.grafana,
  444. })
  445. ).toMatchObject({
  446. signatureType: PluginSignatureType.core,
  447. });
  448. expect(
  449. mapToCatalogPlugin(undefined, {
  450. ...remotePlugin,
  451. versionSignatureType: '',
  452. signatureType: PluginSignatureType.grafana,
  453. })
  454. ).toMatchObject({
  455. signatureType: PluginSignatureType.grafana,
  456. });
  457. // Local only
  458. expect(mapToCatalogPlugin({ ...localPlugin, signatureType: PluginSignatureType.core })).toMatchObject({
  459. signatureType: PluginSignatureType.core,
  460. });
  461. // No local or remote
  462. expect(mapToCatalogPlugin()).toMatchObject({ signatureType: undefined });
  463. });
  464. test('`.updatedAt` - prefers the remote', () => {
  465. // Local & Remote
  466. expect(
  467. mapToCatalogPlugin(
  468. { ...localPlugin, info: { ...localPlugin.info, updated: '2019-01-01' } },
  469. { ...remotePlugin, updatedAt: '2020-01-01' }
  470. )
  471. ).toMatchObject({
  472. updatedAt: '2020-01-01',
  473. });
  474. // Remote only
  475. expect(mapToCatalogPlugin(undefined, { ...remotePlugin, updatedAt: '2020-01-01' })).toMatchObject({
  476. updatedAt: '2020-01-01',
  477. });
  478. // Local only
  479. expect(
  480. mapToCatalogPlugin({ ...localPlugin, info: { ...localPlugin.info, updated: '2019-01-01' } })
  481. ).toMatchObject({
  482. updatedAt: '2019-01-01',
  483. });
  484. // No local or remote
  485. expect(mapToCatalogPlugin()).toMatchObject({ updatedAt: '' });
  486. });
  487. });
  488. describe('sortPlugins()', () => {
  489. test('should be possible to sort by `name` ASC', () => {
  490. const sorted = sortPlugins(
  491. [
  492. getCatalogPluginMock({ id: 'zabbix', name: 'Zabbix' }),
  493. getCatalogPluginMock({ id: 'snowflake', name: 'Snowflake' }),
  494. getCatalogPluginMock({ id: 'jira', name: 'Jira' }),
  495. getCatalogPluginMock({ id: 'pie-chart', name: 'Pie Chart' }),
  496. getCatalogPluginMock({ id: 'cloud-watch', name: 'CloudWatch' }),
  497. ],
  498. Sorters.nameAsc
  499. );
  500. expect(sorted.map(({ name }) => name)).toEqual(['CloudWatch', 'Jira', 'Pie Chart', 'Snowflake', 'Zabbix']);
  501. });
  502. test('should be possible to sort by `name` DESC', () => {
  503. const sorted = sortPlugins(
  504. [
  505. getCatalogPluginMock({ id: 'zabbix', name: 'Zabbix' }),
  506. getCatalogPluginMock({ id: 'snowflake', name: 'Snowflake' }),
  507. getCatalogPluginMock({ id: 'jira', name: 'Jira' }),
  508. getCatalogPluginMock({ id: 'pie-chart', name: 'Pie Chart' }),
  509. getCatalogPluginMock({ id: 'cloud-watch', name: 'CloudWatch' }),
  510. ],
  511. Sorters.nameDesc
  512. );
  513. expect(sorted.map(({ name }) => name)).toEqual(['Zabbix', 'Snowflake', 'Pie Chart', 'Jira', 'CloudWatch']);
  514. });
  515. test('should be possible to sort by `updated` (latest first)', () => {
  516. const sorted = sortPlugins(
  517. [
  518. getCatalogPluginMock({ id: 'zabbix', updatedAt: '2010-01-01' }),
  519. getCatalogPluginMock({ id: 'snowflake', updatedAt: '2012-01-01' }),
  520. getCatalogPluginMock({ id: 'jira', updatedAt: '2005-01-01' }),
  521. getCatalogPluginMock({ id: 'pie-chart', updatedAt: '2021-01-01' }),
  522. getCatalogPluginMock({ id: 'cloud-watch', updatedAt: '2009-01-01' }),
  523. ],
  524. Sorters.updated
  525. );
  526. expect(sorted.map(({ id }) => id)).toEqual(['pie-chart', 'snowflake', 'zabbix', 'cloud-watch', 'jira']);
  527. });
  528. test('should be possible to sort by `published` (latest first)', () => {
  529. const sorted = sortPlugins(
  530. [
  531. getCatalogPluginMock({ id: 'zabbix', publishedAt: '2010-01-01' }),
  532. getCatalogPluginMock({ id: 'snowflake', publishedAt: '2012-01-01' }),
  533. getCatalogPluginMock({ id: 'jira', publishedAt: '2005-01-01' }),
  534. getCatalogPluginMock({ id: 'pie-chart', publishedAt: '2021-01-01' }),
  535. getCatalogPluginMock({ id: 'cloud-watch', publishedAt: '2009-01-01' }),
  536. ],
  537. Sorters.published
  538. );
  539. expect(sorted.map(({ id }) => id)).toEqual(['pie-chart', 'snowflake', 'zabbix', 'cloud-watch', 'jira']);
  540. });
  541. test('should be possible to sort by `downloads` (greatest first)', () => {
  542. const sorted = sortPlugins(
  543. [
  544. getCatalogPluginMock({ id: 'zabbix', downloads: 30 }),
  545. getCatalogPluginMock({ id: 'snowflake', downloads: 10 }),
  546. getCatalogPluginMock({ id: 'jira', downloads: 100 }),
  547. getCatalogPluginMock({ id: 'pie-chart', downloads: 350 }),
  548. getCatalogPluginMock({ id: 'cloud-watch', downloads: 200 }),
  549. ],
  550. Sorters.downloads
  551. );
  552. expect(sorted.map(({ id }) => id)).toEqual(['pie-chart', 'cloud-watch', 'jira', 'zabbix', 'snowflake']);
  553. });
  554. });
  555. describe('isLocalPluginVisible()', () => {
  556. test('should return TRUE if the plugin is not listed as hidden in the main Grafana configuration', () => {
  557. config.pluginCatalogHiddenPlugins = ['akumuli-datasource'];
  558. const plugin = getLocalPluginMock({
  559. id: 'barchart',
  560. });
  561. expect(isLocalPluginVisible(plugin)).toBe(true);
  562. });
  563. test('should return FALSE if the plugin is listed as hidden in the main Grafana configuration', () => {
  564. config.pluginCatalogHiddenPlugins = ['akumuli-datasource'];
  565. const plugin = getLocalPluginMock({
  566. id: 'akumuli-datasource',
  567. });
  568. expect(isLocalPluginVisible(plugin)).toBe(false);
  569. });
  570. });
  571. describe('isRemotePluginVisible()', () => {
  572. test('should return TRUE if the plugin is not listed as hidden in the main Grafana configuration', () => {
  573. config.pluginCatalogHiddenPlugins = ['akumuli-datasource'];
  574. const plugin = getRemotePluginMock({
  575. slug: 'barchart',
  576. });
  577. expect(isRemotePluginVisible(plugin)).toBe(true);
  578. });
  579. test('should return FALSE if the plugin is listed as hidden in the main Grafana configuration', () => {
  580. config.pluginCatalogHiddenPlugins = ['akumuli-datasource'];
  581. const plugin = getRemotePluginMock({
  582. slug: 'akumuli-datasource',
  583. });
  584. expect(isRemotePluginVisible(plugin)).toBe(false);
  585. });
  586. });
  587. });