123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import { jest } from '@jest/globals';
- import RemoteCode from '../remoteCode';
- const REMOTE = 'http://localhost:5000/';
- const SITEID = 'mysiteid';
- describe('Remote Test', () => {
- let remoteCode;
- let preProcess;
- beforeEach(() => {
- preProcess = jest.fn((script) => {
- return script;
- });
- remoteCode = new RemoteCode({
- disableCache: false,
- preProcess,
- });
- });
- it('should call remote function', async () => {
- const content = { hello: true };
- const result = await remoteCode.exec(SITEID, REMOTE, 'scripts/mysetup.js', {
- content,
- });
- expect(result).toEqual('foo');
- expect(content).toEqual(
- expect.objectContaining({ hello: true, response: 42 })
- );
- // Hit cache
- const result2 = await remoteCode.exec(
- SITEID,
- REMOTE,
- 'scripts/mysetup.js',
- {
- content,
- }
- );
- expect(result2).toEqual('foo');
- // Clear cache
- remoteCode.clearCache(REMOTE);
- const result3 = await remoteCode.exec(
- SITEID,
- REMOTE,
- 'scripts/mysetup.js',
- {
- content,
- }
- );
- expect(result3).toEqual('foo');
- });
- it('should filter requirements', async () => {
- const http = await remoteCode.exec(
- SITEID,
- REMOTE,
- 'scripts/mysetupWithRequire.js'
- );
- const httpReal = await import('http');
- expect(http['STATUS_CODES']).toEqual(httpReal['STATUS_CODES']);
- try {
- await remoteCode.exec(SITEID, REMOTE, 'scripts/mysetupWithBadRequire.js');
- } catch (e) {
- expect(e.code).toMatch('ENOTFOUND');
- }
- });
- it("shouldn't call missing remote function", async () => {
- try {
- await remoteCode.exec(SITEID, REMOTE, 'notexisting');
- } catch (e) {
- expect(e).toMatch(
- 'Script notexisting not found on remote http://localhost:5000'
- );
- }
- });
- });
|