ice/packages/miniapp-react-dom/tests/props.spec.jsx

92 lines
3.7 KiB
React
Raw Permalink Normal View History

feat: miniapp (#333) * feat(miniapp): use platform in cli to generate miniapp code * fix: lint error * feat(miniapp): add miniapp project * chore: revert example code * chore: optimize code * feat(miniapp): migrate some packages to ice * feat(miniapp): update swcOptions for miniapp * chore(example): add more pages * feat(miniapp): add miniapp runClientApp * feat(miniapp): update compile config for miniapp * feat(miniapp): support get app and routes config in compile * fix(miniapp): lint warning * feat(miniapp): support routeData/routeConfig and reduce size * fix: lint warning * fix: lint warning * feat(miniapp): support global css and page css * feat(miniapp): support css modules * feat(miniapp): support copying assets in public dir * feat(miniapp): support Link and getSearchParams * feat(miniapp): use index.ts.ejs as template * feat(miniapp): support passing params in Link * feat(miniapp): support history in miniapp * fix(miniapp): build without compiling server bundle * fix(miniapp): use getRoutesConfig * feat(miniapp): support build options like outputDir * chore(miniapp): update miniapp examples * feat(miniapp): support html tags * feat(miniapp): remove taro related code * fix(miniapp): some cases * fix(miniapp): use require.resolve to get page loader * chore(miniapp): update lock * fix(miniapp): page loader check this name * chore(miniapp): add devDependencies * chore: update index ejs template * refactor(miniapp): update multiple platforms logic in ice * chore: lock regenerator-runtime version * chore(miniapp): add ali miniapp build command * fix(ice): set default value of platform to web * fix(runtime): remove duplicated denpdencies * chore: set default value of platform to web * feat(miniapp): registerSerialization when plugin activated * test(miniapp): suit for vitest * chore(miniapp): add webpack-sources to bundles * chore: clean code * chore(miniapp): remove jest config file * fix: lint error * fix: add default value of outputDir * refactor(miniapp): remove openBrowser from logMessage * chore(miniapp): remove unnecessary ts-ignore * fix: type error * chore(miniapp): rename Adapter to adapter * chore(miniapp): update interface name * refactor: use plugins to support miniapp * chore(miniapp): update example name * fix: update dependencies * feat(miniapp): add platform check in cli * chore: update lock * fix: remove useless platform option * fix: lint warning * fix: lint warning * test: use src code in tests * chore(miniapp): remove ||= operators * fix(miniapp): exclude miniapp-runtime esm code * fix(ice-cli): update platform for build command
2022-09-23 11:11:38 +08:00
import { expect, describe, test, vi } from 'vitest';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as React from 'react';
import { document, createEvent } from '@ice/miniapp-runtime';
import { render } from '../src/index';
describe('Context', () => {
describe('setValueOnElement', () => {
test('should set values as properties by default', () => {
const container = document.createElement('div');
render(<div title="Tip!" />, container);
expect(container.firstChild.getAttribute('title')).toBe('Tip!');
});
test('should set values as attributes if necessary', () => {
const container = document.createElement('div');
render(<div role="#" />, container);
expect(container.firstChild.getAttribute('role')).toBe('#');
expect(container.firstChild.role).toBeUndefined();
});
test('should set values as attributes for specific props', () => {
const container = document.createElement('div');
render(<view type="button" id="test" />, container);
expect(container.firstChild.getAttribute('type')).toBe('button');
expect(container.firstChild.id).toBe('test');
});
test('className should works as class', () => {
const container = document.createElement('div');
render(<input type="button" className="test" />, container);
expect(container.firstChild.getAttribute('class')).toBe('test');
});
test('string style', () => {
const container = document.createElement('div');
render(<input type="button" style="color: red" />, container);
expect(container.firstChild.getAttribute('style')).toBe('color: red;');
});
test('object style', () => {
const container = document.createElement('div');
render(<input type="button" style={{ color: 'red' }} />, container);
expect(container.firstChild.getAttribute('style')).toBe('color: red;');
});
test('set style as number', () => {
const container = document.createElement('div');
render(<input type="button" style={{ fontSize: 14 }} />, container);
expect(container.firstChild.getAttribute('style')).toBe('font-size: 14px;');
});
test('onClick should work like onTap', () => {
const container = document.createElement('div');
const spy = vi.fn();
render(<view type="button" onClick={spy} />, container);
expect('tap' in container.firstChild.__handlers).toBe(true);
});
test('can dispatch event', () => {
const container = document.createElement('div');
const spy = vi.fn();
render(<view type="button" onClick={spy} id="fork" />, container);
const event = createEvent({ type: 'tap', currentTarget: { id: container.firstChild.uid }, target: { id: container.firstChild.uid } });
container.firstChild.dispatchEvent(event);
expect(spy).toBeCalled();
});
test('should patch properies properly', () => {
const container = document.createElement('div');
render(<div id="1" />, container);
render(<div id="2" />, container);
expect(container.firstChild.id).toBe('2');
});
test('should patch properies properly 2', () => {
const container = document.createElement('div');
render(<div id="1" a="a" />, container);
render(<div id="2" b="b" />, container);
expect(container.firstChild.id).toBe('2');
expect(container.firstChild.getAttribute('a')).toBe('');
expect(container.firstChild.getAttribute('b')).toBe('b');
});
test('should ignore ref', () => {
const container = document.createElement('div');
render(<div ref={React.createRef} />, container);
expect(container.firstChild.getAttribute('ref')).toBe('');
});
});
});