remoteCode.test.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { jest } from '@jest/globals';
  2. import RemoteCode from '../remoteCode';
  3. const REMOTE = 'http://localhost:5000/';
  4. const SITEID = 'mysiteid';
  5. describe('Remote Test', () => {
  6. let remoteCode;
  7. let preProcess;
  8. beforeEach(() => {
  9. preProcess = jest.fn((script) => {
  10. return script;
  11. });
  12. remoteCode = new RemoteCode({
  13. disableCache: false,
  14. preProcess,
  15. });
  16. });
  17. it('should call remote function', async () => {
  18. const content = { hello: true };
  19. const result = await remoteCode.exec(SITEID, REMOTE, 'scripts/mysetup.js', {
  20. content,
  21. });
  22. expect(result).toEqual('foo');
  23. expect(content).toEqual(
  24. expect.objectContaining({ hello: true, response: 42 })
  25. );
  26. // Hit cache
  27. const result2 = await remoteCode.exec(
  28. SITEID,
  29. REMOTE,
  30. 'scripts/mysetup.js',
  31. {
  32. content,
  33. }
  34. );
  35. expect(result2).toEqual('foo');
  36. // Clear cache
  37. remoteCode.clearCache(REMOTE);
  38. const result3 = await remoteCode.exec(
  39. SITEID,
  40. REMOTE,
  41. 'scripts/mysetup.js',
  42. {
  43. content,
  44. }
  45. );
  46. expect(result3).toEqual('foo');
  47. });
  48. it('should filter requirements', async () => {
  49. const http = await remoteCode.exec(
  50. SITEID,
  51. REMOTE,
  52. 'scripts/mysetupWithRequire.js'
  53. );
  54. const httpReal = await import('http');
  55. expect(http['STATUS_CODES']).toEqual(httpReal['STATUS_CODES']);
  56. try {
  57. await remoteCode.exec(SITEID, REMOTE, 'scripts/mysetupWithBadRequire.js');
  58. } catch (e) {
  59. expect(e.code).toMatch('ENOTFOUND');
  60. }
  61. });
  62. it("shouldn't call missing remote function", async () => {
  63. try {
  64. await remoteCode.exec(SITEID, REMOTE, 'notexisting');
  65. } catch (e) {
  66. expect(e).toMatch(
  67. 'Script notexisting not found on remote http://localhost:5000'
  68. );
  69. }
  70. });
  71. });