Skip to content

Commit

Permalink
fix: add mockSandbox at unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghangit committed Dec 21, 2023
1 parent 975bb6d commit e59576f
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions packages/shared/src/assets-transpilers/__tests__/script.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import { expect, it } from 'vitest';
import { expect, it, vi } from 'vitest';
import transpileScript from '../script';

it('inline script not include sourceURL', () => {
class MockSandbox {
id = 'testApp';
constantIntrinsicNames = [];
makeEvaluateFactory(source: string, sourceURL?: string): string {
const sourceMapURL = sourceURL ? `//# sourceURL=${sourceURL}\n` : '';
const globalObjectOptimizer = this.constantIntrinsicNames.length
? `const {${this.constantIntrinsicNames.join(',')}} = this;`
: '';
// eslint-disable-next-line max-len
return `;(function(){with(this){${globalObjectOptimizer}${source}\n${sourceMapURL}}}).bind(window.${this.id})();`;
return '';
}
}

const code = 'console.log("hello world")';
const publicPath = 'http://localhost:8000';
const scriptElement = document.createElement('script');
scriptElement.innerHTML = 'console.log("hello world")';
scriptElement.innerHTML = code;
const sandboxInstance = new MockSandbox();
const publicPath = 'http://localhost:8000';
const transpiledScriptElement = transpileScript(scriptElement, publicPath, {
const makeEvaluateFactorySpy = vi.spyOn(sandboxInstance, 'makeEvaluateFactory');
transpileScript(scriptElement, publicPath, {
fetch: window.fetch,
rawNode: scriptElement,
sandbox: sandboxInstance,
});
expect(transpiledScriptElement.innerHTML).toEqual(expect.not.stringContaining(`//# sourceURL=${publicPath}`));
expect(makeEvaluateFactorySpy).toHaveBeenCalledWith(code);
});

0 comments on commit e59576f

Please sign in to comment.