Files
Investidor-V2/tests/frontend/interest-draft-storage.test.mjs
T
2026-11-02 20:31:15 +02:00

73 lines
2.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { compileFunction } from 'node:vm';
import ts from 'typescript';
await test('blocked browser storage cannot prevent authentication or clearing a submitted interest', () => {
const source = fs.readFileSync(
new URL(
'../../resources/js/hooks/use-interest-draft.ts',
import.meta.url,
),
'utf8',
);
const output = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
},
}).outputText;
const module = { exports: {} };
let destination;
const storage = {
getItem() {
throw new Error('blocked');
},
setItem() {
throw new Error('blocked');
},
removeItem() {
throw new Error('blocked');
},
};
compileFunction(output, [
'require',
'module',
'exports',
'sessionStorage',
'window',
'location',
])(
(name) =>
name === 'react'
? {
useState: (init) => [init(), () => {}],
useEffect: (effect) => effect(),
}
: { newSubmissionKey: () => 'synthetic-key' },
module,
module.exports,
storage,
{
location: {
assign: (url) => {
destination = url;
},
},
},
{ pathname: '/oportunidades/teste', search: '?sector=1' },
);
const draft = module.exports.useInterestDraft('synthetic', {
name: 'Synthetic',
email: 'synthetic@example.invalid',
});
assert.equal(draft.key, 'synthetic-key');
assert.doesNotThrow(() => draft.clear());
assert.doesNotThrow(() => draft.authenticate());
assert.equal(
destination,
'/participar/entrar?return=%2Foportunidades%2Fteste%3Fsector%3D1',
);
});