Skip to content

Commit 00a372a

Browse files
committed
add tests, fix register
1 parent 5a0c0b3 commit 00a372a

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.6",
2+
"version": "0.1.7",
33
"license": "MIT",
44
"repository": {
55
"type": "git",
@@ -39,6 +39,7 @@
3939
"@types/react": "^16.9.23",
4040
"@types/react-dom": "^16.9.5",
4141
"husky": "^4.2.5",
42+
"mutationobserver-shim": "^0.3.7",
4243
"tsdx": "^0.13.2",
4344
"tslib": "^2.0.0",
4445
"typescript": "^3.9.7"

src/abode.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export let componentSelector = 'data-component';
2222
export let components: RegisteredComponents = {};
2323
export let unPopulatedElements: Element[] = [];
2424

25-
export const register = async (name: string, fn: () => Promise<any>) => {
26-
components[name] = await retry(fn, 10, 20);
25+
export const register = (name: string, fn: () => Promise<any>) => {
26+
components[name] = retry(fn, 10, 20);
2727
};
2828

2929
export const unRegisterAllComponents = () => {
@@ -125,10 +125,12 @@ export const renderAbode = async (el: Element) => {
125125
};
126126

127127
export const trackPropChanges = (el: Element) => {
128-
const observer = new MutationObserver(() => {
129-
renderAbode(el);
130-
});
131-
observer.observe(el, { attributes: true });
128+
if (MutationObserver) {
129+
const observer = new MutationObserver(() => {
130+
renderAbode(el);
131+
});
132+
observer.observe(el, { attributes: true });
133+
}
132134
};
133135

134136
export const update = async (

test/abode.test.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import {
99
register,
1010
unRegisterAllComponents,
1111
components,
12+
populate,
13+
delay,
1214
} from '../src/abode';
1315

14-
describe('getCleanPropName', () => {});
16+
import 'mutationobserver-shim';
17+
global.MutationObserver = window.MutationObserver;
1518

1619
describe('helper functions', () => {
1720
beforeEach(() => (document.getElementsByTagName('html')[0].innerHTML = ''));
@@ -96,15 +99,35 @@ describe('exported functions', () => {
9699
});
97100

98101
it('register', async () => {
99-
await register('TestComponent', () => import('./TestComponent'));
102+
register('TestComponent', () => import('./TestComponent'));
100103
expect(Object.keys(components)).toEqual(['TestComponent']);
101104
expect(Object.values(components).length).toEqual(1);
102-
expect(Object.keys(Object.values(components)[0])).toEqual(['default']);
105+
const promise = Object.values(components)[0];
106+
expect(typeof promise.then).toEqual('function');
107+
const module = await promise;
108+
expect(Object.keys(module)).toEqual(['default']);
109+
});
110+
111+
it('populate', async () => {
112+
const abodeElement = document.createElement('div');
113+
abodeElement.setAttribute('data-component', 'TestComponent');
114+
document.body.appendChild(abodeElement);
115+
expect(document.body.innerHTML).toEqual(
116+
`<div data-component="TestComponent"></div>`
117+
);
118+
119+
register('TestComponent', () => import('./TestComponent'));
120+
populate();
121+
122+
await delay(20);
123+
124+
expect(document.body.innerHTML).toEqual(
125+
`<div data-component="TestComponent" react-abode-populated="true"><div>testing 1 2 3 </div></div>`
126+
);
103127
});
104128

105129
it.skip('getActiveComponents', () => {});
106130
it.skip('setComponentSelector', () => {});
107131
it.skip('register', () => {});
108-
it.skip('populate', () => {});
109132
it.skip('getSriptProps', () => {});
110133
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4137,6 +4137,11 @@ ms@^2.1.1:
41374137
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
41384138
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
41394139

4140+
mutationobserver-shim@^0.3.7:
4141+
version "0.3.7"
4142+
resolved "https://registry.yarnpkg.com/mutationobserver-shim/-/mutationobserver-shim-0.3.7.tgz#8bf633b0c0b0291a1107255ed32c13088a8c5bf3"
4143+
integrity sha512-oRIDTyZQU96nAiz2AQyngwx1e89iApl2hN5AOYwyxLUB47UYsU3Wv9lJWqH5y/QdiYkc5HQLi23ZNB3fELdHcQ==
4144+
41404145
mute-stream@0.0.8:
41414146
version "0.0.8"
41424147
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"

0 commit comments

Comments
 (0)