-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
975bb6d
commit e59576f
Showing
1 changed file
with
8 additions
and
13 deletions.
There are no files selected for viewing
21 changes: 8 additions & 13 deletions
21
packages/shared/src/assets-transpilers/__tests__/script.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |