The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { HOOKS, type Hooks, Window } from '@remote-dom/polyfill';
const createHookRecorder = () => {
const polyfillWindow = new Window();
const calls: { name: keyof Hooks; args: unknown[] }[] = [];
const hooks = (polyfillWindow as unknown as Record<symbol, Partial<Hooks>>)[
HOOKS
];
const recordedHookNames: (keyof Hooks)[] = [
'createElement',
'createText',
'setText',
'setAttribute',
'removeAttribute',
'insertChild',
'removeChild',
];
for (const hookName of recordedHookNames) {
hooks[hookName] = (...args: unknown[]) => {
calls.push({ name: hookName, args });
};
}
return {
document: polyfillWindow.document as unknown as Document,
callsNamed: (hookName: keyof Hooks) =>
calls.filter((call) => call.name === hookName),
clearCalls: () => {
calls.length = 0;
},
callNames: () => calls.map((call) => call.name),
};
};
describe('@remote-dom/polyfill mutation hooks contract the worker MutationObserver is built on, which a remote-dom upgrade must not silently break', () => {
it('exports HOOKS as a symbol keying a mutable hooks object', () => {
const polyfillWindow = new Window();
expect(typeof HOOKS).toBe('symbol');
expect(
typeof (polyfillWindow as unknown as Record<symbol, unknown>)[HOOKS],
).toBe('object');
});
it('still ships MutationObserver without an observe method', () => {
const polyfillWindow = new Window();
expect(
polyfillWindow.MutationObserver.prototype as unknown as Record<
string,
unknown
>,
).not.toHaveProperty('observe');
});
it('calls createElement when an element is created', () => {
const { document, callsNamed } = createHookRecorder();
const element = document.createElement('div');
expect(callsNamed('createElement')).toHaveLength(1);
expect(callsNamed('createElement')[0].args[0]).toBe(element);
});
it('calls createText and setText with the text node and its data', () => {
const { document, callsNamed } = createHookRecorder();
const text = document.createTextNode('first');
text.data = 'second';
expect(callsNamed('createText')[0].args[0]).toBe(text);
expect(callsNamed('createText')[0].args[1]).toBe('first');
expect(callsNamed('setText')[0].args[0]).toBe(text);
expect(callsNamed('setText')[0].args[1]).toBe('second');
});
it('calls setAttribute and removeAttribute with the element and attribute name', () => {
const { document, callsNamed } = createHookRecorder();
const element = document.createElement('div');
element.setAttribute('title', 'hello');
element.removeAttribute('title');
expect(callsNamed('setAttribute')[0].args[0]).toBe(element);
expect(callsNamed('setAttribute')[0].args[1]).toBe('title');
expect(callsNamed('setAttribute')[0].args[2]).toBe('hello');
expect(callsNamed('removeAttribute')[0].args[0]).toBe(element);
expect(callsNamed('removeAttribute')[0].args[1]).toBe('title');
});
it('calls insertChild and removeChild with the parent, the node and its index', () => {
const { document, callsNamed } = createHookRecorder();
const parent = document.createElement('div');
const firstChild = document.createElement('span');
const secondChild = document.createElement('span');
parent.appendChild(firstChild);
parent.appendChild(secondChild);
parent.removeChild(secondChild);
expect(callsNamed('insertChild')[1].args[0]).toBe(parent);
expect(callsNamed('insertChild')[1].args[1]).toBe(secondChild);
expect(callsNamed('insertChild')[1].args[2]).toBe(1);
expect(callsNamed('removeChild')[0].args[0]).toBe(parent);
expect(callsNamed('removeChild')[0].args[1]).toBe(secondChild);
expect(callsNamed('removeChild')[0].args[2]).toBe(1);
});
it('calls removeChild then insertChild when an attached node moves', () => {
const { document, callNames, clearCalls } = createHookRecorder();
const parent = document.createElement('div');
const firstChild = document.createElement('span');
const secondChild = document.createElement('span');
parent.appendChild(firstChild);
parent.appendChild(secondChild);
clearCalls();
parent.insertBefore(secondChild, firstChild);
expect(callNames()).toEqual(['removeChild', 'insertChild']);
});
it('skips insertChild and removeChild when the parent is not an element node', () => {
const { document, callsNamed } = createHookRecorder();
const fragment = document.createDocumentFragment();
const child = document.createElement('span');
fragment.appendChild(child);
fragment.removeChild(child);
expect(callsNamed('insertChild')).toHaveLength(0);
expect(callsNamed('removeChild')).toHaveLength(0);
});
it('does not call createText for a comment but does call setText when its data changes', () => {
const { document, callsNamed } = createHookRecorder();
const comment = document.createComment('first');
expect(callsNamed('createText')).toHaveLength(0);
comment.data = 'second';
expect(callsNamed('setText')).toHaveLength(1);
expect(callsNamed('setText')[0].args[0]).toBe(comment);
expect(callsNamed('setText')[0].args[1]).toBe('second');
});
it.each([
['Node', 'compareDocumentPosition'],
['Node', 'getRootNode'],
['Element', 'closest'],
['Element', 'matches'],
['Element', 'focus'],
['Element', 'blur'],
])(
'still ships %s without %s, which the worker polyfills install itself',
(className, methodName) => {
const polyfillWindow = new Window() as unknown as Record<
string,
{ prototype: Record<string, unknown> }
>;
expect(polyfillWindow[className].prototype).not.toHaveProperty(
methodName,
);
},
);
it('still ships the Node.contains walk that re-reads the argument parent, which the worker replaces', () => {
const polyfillWindow = new Window();
expect(String(polyfillWindow.Node.prototype.contains)).toContain(
'node.parentNode',
);
});
it('still throws on pseudo-class selectors, which the worker selector engine replaces', () => {
const polyfillWindow = new Window();
const { document } = polyfillWindow;
document.body.append(document.createElement('button'));
expect(() => document.body.querySelectorAll(':disabled')).toThrow(
'not implemented',
);
});
it('exposes childNodes as a list whose item returns undefined out of range', () => {
const { document } = createHookRecorder();
const parent = document.createElement('div');
const child = document.createElement('span');
parent.appendChild(child);
expect(parent.childNodes.item(0)).toBe(child);
expect(parent.childNodes.item(-1)).toBeUndefined();
expect(parent.childNodes.item(1)).toBeUndefined();
});
});