1
0

graphiql-plugin.e2e-spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { ConfigService, LanguageCode, mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  5. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  6. import { GraphiQLService } from '../src/graphiql.service';
  7. import { GraphiqlPlugin } from '../src/plugin';
  8. describe('GraphiQLPlugin', () => {
  9. let serviceInstance: GraphiQLService;
  10. let configService: ConfigService;
  11. const { server, adminClient } = createTestEnvironment(
  12. mergeConfig(testConfig(), {
  13. apiOptions: {
  14. adminApiPlayground: true,
  15. shopApiPlayground: true,
  16. },
  17. plugins: [GraphiqlPlugin.init()],
  18. }),
  19. );
  20. beforeAll(async () => {
  21. await server.init({
  22. initialData: {
  23. defaultLanguage: LanguageCode.en,
  24. defaultZone: 'Europe/London',
  25. countries: [],
  26. taxRates: [],
  27. paymentMethods: [],
  28. shippingMethods: [],
  29. collections: [],
  30. },
  31. });
  32. await adminClient.asSuperAdmin();
  33. configService = server.app.get(ConfigService);
  34. serviceInstance = server.app.get(GraphiQLService);
  35. }, TEST_SETUP_TIMEOUT_MS);
  36. afterAll(async () => {
  37. await server.destroy();
  38. });
  39. describe('configuration', () => {
  40. it('should disable GraphQL playground in config', async () => {
  41. expect(configService.apiOptions.adminApiPlayground).toBe(false);
  42. expect(configService.apiOptions.shopApiPlayground).toBe(false);
  43. });
  44. });
  45. describe('GraphiQLService', () => {
  46. describe('getAdminApiUrl', () => {
  47. it('should return the admin API URL', () => {
  48. configService.apiOptions.adminApiPath = 'admin-api';
  49. const url = serviceInstance.getAdminApiUrl();
  50. expect(url).toBe('/admin-api');
  51. });
  52. it('should use default path if not specified', () => {
  53. configService.apiOptions.adminApiPath = 'admin-api';
  54. const url = serviceInstance.getAdminApiUrl();
  55. expect(url).toBe('/admin-api');
  56. });
  57. });
  58. describe('getShopApiUrl', () => {
  59. it('should return the shop API URL', () => {
  60. configService.apiOptions.shopApiPath = 'shop-api';
  61. const url = serviceInstance.getShopApiUrl();
  62. expect(url).toBe('/shop-api');
  63. });
  64. it('should use default path if not specified', () => {
  65. configService.apiOptions.shopApiPath = 'shop-api';
  66. const url = serviceInstance.getShopApiUrl();
  67. expect(url).toBe('/shop-api');
  68. });
  69. });
  70. describe('createApiUrl', () => {
  71. it('should create a relative URL if no host is specified', () => {
  72. configService.apiOptions.hostname = '';
  73. configService.apiOptions.port = 3000;
  74. const url = (serviceInstance as any).createApiUrl('test-api');
  75. expect(url).toBe('/test-api');
  76. });
  77. it('should create an absolute URL if host is specified', () => {
  78. configService.apiOptions.hostname = 'example.com';
  79. configService.apiOptions.port = 3000;
  80. const url = (serviceInstance as any).createApiUrl('test-api');
  81. expect(url).toBe('http://example.com:3000/test-api');
  82. });
  83. it('should handle HTTPS hosts', () => {
  84. configService.apiOptions.hostname = 'https://example.com';
  85. configService.apiOptions.port = 443;
  86. const url = (serviceInstance as any).createApiUrl('test-api');
  87. expect(url).toBe('https://example.com:443/test-api');
  88. });
  89. it('should handle paths with leading slash', () => {
  90. configService.apiOptions.hostname = 'example.com';
  91. configService.apiOptions.port = 3000;
  92. const url = (serviceInstance as any).createApiUrl('/test-api');
  93. expect(url).toBe('http://example.com:3000/test-api');
  94. });
  95. });
  96. });
  97. });