validate.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { GrowiPluginType } from '@growi/core/dist/consts';
  2. import examplePkg from '^/test/fixtures/example-package/template1/package.json';
  3. import { validatePackageJson } from './validate';
  4. const mocks = vi.hoisted(() => {
  5. return {
  6. importPackageJsonMock: vi.fn(),
  7. };
  8. });
  9. vi.mock('./import', () => {
  10. return { importPackageJson: mocks.importPackageJsonMock };
  11. });
  12. describe('validatePackageJson()', () => {
  13. it('returns a data object', async() => {
  14. // setup
  15. mocks.importPackageJsonMock.mockResolvedValue(examplePkg);
  16. // when
  17. const data = await validatePackageJson('package.json');
  18. // then
  19. expect(data).not.toBeNull();
  20. });
  21. it("with the 'expectedPluginType' argument returns a data object", async() => {
  22. // setup
  23. mocks.importPackageJsonMock.mockResolvedValue(examplePkg);
  24. // when
  25. const data = await validatePackageJson('package.json', GrowiPluginType.Template);
  26. // then
  27. expect(data).not.toBeNull();
  28. });
  29. describe('should throw an GrowiPluginValidationError', () => {
  30. it("when the pkg does not have 'growiPlugin' directive", async() => {
  31. // setup
  32. mocks.importPackageJsonMock.mockResolvedValue({});
  33. // when
  34. const caller = async() => { await validatePackageJson('package.json') };
  35. // then
  36. await expect(caller).rejects.toThrow("The package.json does not have 'growiPlugin' directive.");
  37. });
  38. it("when the 'schemaVersion' is NaN", async() => {
  39. // setup
  40. mocks.importPackageJsonMock.mockResolvedValue({
  41. growiPlugin: {
  42. schemaVersion: 'foo',
  43. },
  44. });
  45. // when
  46. const caller = async() => { await validatePackageJson('package.json') };
  47. // then
  48. await expect(caller).rejects.toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
  49. });
  50. it("when the 'schemaVersion' is less than 4", async() => {
  51. // setup
  52. mocks.importPackageJsonMock.mockResolvedValue({
  53. growiPlugin: {
  54. schemaVersion: 3,
  55. },
  56. });
  57. // when
  58. const caller = async() => { await validatePackageJson('package.json') };
  59. // then
  60. await expect(caller).rejects.toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
  61. });
  62. it("when the 'types' directive does not exist", async() => {
  63. // setup
  64. mocks.importPackageJsonMock.mockResolvedValue({
  65. growiPlugin: {
  66. schemaVersion: 4,
  67. },
  68. });
  69. // when
  70. const caller = async() => { await validatePackageJson('package.json') };
  71. // then
  72. await expect(caller).rejects.toThrow("The growiPlugin directive does not have 'types' directive.");
  73. });
  74. it("when the 'types' directive does not have expected plugin type", async() => {
  75. // setup
  76. mocks.importPackageJsonMock.mockResolvedValue({
  77. growiPlugin: {
  78. schemaVersion: 4,
  79. types: [GrowiPluginType.Template],
  80. },
  81. });
  82. // when
  83. const caller = async() => { await validatePackageJson('package.json', GrowiPluginType.Script) };
  84. // then
  85. await expect(caller).rejects.toThrow("The growiPlugin directive does not have expected plugin type in 'types' directive.");
  86. });
  87. });
  88. });