Skip to content

Commit 54808ee

Browse files
committed
test: clean up tests
1 parent baaa4c0 commit 54808ee

12 files changed

Lines changed: 112 additions & 92 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class AppService {
124124
@Inject() userService: UserService;
125125

126126
// injected with symbol
127-
@Inject(ApplicationRef) userService: Application;
127+
@Inject(ChangeDetectorRef) userService: ChangeDetector;
128128
}
129129
```
130130

src/application.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/bootstrap.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Provider, Injector } from '@homebots/injector';
1+
import { Provider, Injector, TreeInjector } from '@homebots/injector';
22
import { domReady } from './utils';
3-
import { Application } from './application';
4-
import { ChangeDetectorRef } from './change-detection/change-detection';
3+
import { ChangeDetector, ChangeDetectorRef } from './change-detection/change-detection';
54
import { ReactiveChangeDetector } from './change-detection/reactive-change-detector';
65
import { ForContainer } from './containers/for-container';
76
import { IfContainer } from './containers/if-container';
@@ -25,14 +24,20 @@ export interface BootstrapOptions {
2524
useShadowDom?: boolean;
2625
}
2726

27+
export interface Application {
28+
injector: Injector;
29+
changeDetector: ChangeDetector;
30+
check(): Promise<void>;
31+
}
32+
2833
export class Bootstrap {
2934
private static promise: Promise<unknown> = domReady().then(() => Bootstrap.addDefaultRules());
3035

3136
static whenReady(fn: (...args: any[]) => any) {
3237
return (this.promise = this.promise.then(fn));
3338
}
3439

35-
static createApplication(rootNode: HTMLElement = document.body, options?: BootstrapOptions) {
40+
static createApplication(rootNode: HTMLElement = document.body, options?: BootstrapOptions): Application {
3641
options = {
3742
...defaultOptions,
3843
...options,
@@ -41,14 +46,33 @@ export class Bootstrap {
4146
const { providers } = options;
4247
providers.unshift(defaultChangeDetector);
4348

44-
const application = new Application(rootNode, providers);
45-
Bootstrap.whenReady(() => application.check());
49+
const injector = Bootstrap.setupInjector(rootNode, providers);
50+
const changeDetector = injector.get(ChangeDetectorRef);
4651

4752
if (options.useShadowDom !== undefined) {
48-
application.injector.get(ShadowDomToggle).toggle(options.useShadowDom);
53+
injector.get(ShadowDomToggle).toggle(options.useShadowDom);
4954
}
5055

51-
return application;
56+
const app = {
57+
injector,
58+
changeDetector,
59+
check() {
60+
return changeDetector.markAsDirtyAndCheck();
61+
},
62+
};
63+
64+
Bootstrap.whenReady(() => app.check());
65+
66+
return app;
67+
}
68+
69+
static setupInjector(rootNode: HTMLElement, providers: Provider[] = []) {
70+
const injector = new TreeInjector();
71+
72+
Injector.setInjectorOf(rootNode, injector);
73+
injector.provideAll(providers);
74+
75+
return injector;
5276
}
5377

5478
static addDefaultRules() {

src/change-detection/change-detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface ChangeDetector {
3737

3838
markAsDirtyAndCheck(): Promise<void>;
3939
markTreeForCheck(): void;
40-
scheduleTreeCheck(): void;
40+
scheduleTreeCheck(options?: { async: boolean }): void;
4141
check(): void;
4242
checkTree(): void;
4343
unregister(): void;

src/change-detection/reactive-change-detector.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ export class ReactiveChangeDetector implements ChangeDetector {
7373
}
7474
}
7575

76-
async markAsDirtyAndCheck() {
76+
markAsDirtyAndCheck() {
7777
this.markTreeForCheck();
78-
return void this.scheduleTreeCheck();
78+
return this.scheduleTreeCheck();
7979
}
8080

8181
check() {
@@ -133,9 +133,13 @@ export class ReactiveChangeDetector implements ChangeDetector {
133133
this.children.forEach((cd) => cd.checkTree());
134134
}
135135

136-
async scheduleTreeCheck() {
136+
scheduleTreeCheck(options: { async?: boolean } = {}) {
137137
if (this.root !== this) {
138-
return this.root.scheduleTreeCheck();
138+
return this.root.scheduleTreeCheck(options);
139+
}
140+
141+
if (!options.async) {
142+
this.checkTree();
139143
}
140144

141145
if (this.timer) {

src/component.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { CustomElement } from '.';
1+
import { CustomElement, CustomHTMLElement } from '.';
22

33
describe('CustomElement', () => {
4-
it('should create a custom element class', () => {
5-
const customElement = CustomElement.create(class extends HTMLElement {}, { tag: 'x-custom' });
6-
const instance = document.createElement('x-custom') as any;
4+
it('should define a custom element class', () => {
5+
const tag = 'x-custom' + Math.random();
6+
CustomElement.define(class extends HTMLElement {}, { tag });
7+
const instance = document.createElement(tag) as CustomHTMLElement;
78

8-
expect(typeof customElement).toBe('function');
9-
expect(instance.onInit).toBe('function');
10-
expect(instance.onDestroy).toBe('function');
11-
expect(instance.onBeforeCheck).toBe('function');
12-
expect(instance.onChanges).toBe('function');
9+
expect(customElements.get(tag)).not.toBeUndefined();
10+
expect(typeof instance.onInit).toBe('function');
11+
expect(typeof instance.onDestroy).toBe('function');
12+
expect(typeof instance.onBeforeCheck).toBe('function');
13+
expect(typeof instance.onChanges).toBe('function');
1314
});
1415
});

src/component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ export class CustomElementInternal {
141141
const parent = options.parentInjector || CustomElementInternal.findParentInjector(component);
142142
injector = new Injector(parent);
143143

144-
145144
const localChangeDetector = parent.get(ChangeDetectorRef).fork(component);
146145
injector.provide(ChangeDetectorRef, Value(localChangeDetector));
147146

src/dom/dom-scanner.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ import { DomScanner } from './dom-scanner';
44
import { ReactiveChangeDetector } from '../change-detection/reactive-change-detector';
55
import { ExecutionContext } from '../execution-context';
66

7-
describe('DomScanner', () => {
7+
fdescribe('DomScanner', () => {
88
afterEach(() => clearDom());
99

1010
it('should replace text markers in a text node with data binding tags', () => {
1111
const node = createHtml('You say {{ youSay }} and I say {{ iSay }}');
12-
inject(DomScanner).scanElement(node, new ReactiveChangeDetector(), new ExecutionContext());
12+
const context = new ExecutionContext();
13+
const cd = new ReactiveChangeDetector();
14+
context.addLocals({ youSay: 'goodbye', iSay: 'hello' });
15+
16+
inject(DomScanner).scanElement(node, cd, context);
17+
cd.scheduleTreeCheck();
18+
19+
// expect(node.textContent.trim()).toBe('');
1320
});
1421
});

src/execution-context.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ExecutionContext, SealedExecutionContext } from '.';
22

3-
describe('ExecutionContext', () => {
3+
fdescribe('ExecutionContext', () => {
44
it('runs an expression with provided locals and element', () => {
55
const context = new ExecutionContext(null);
66
context.addLocals({ number: 123, string: 'alice' });
@@ -67,15 +67,16 @@ describe('ExecutionContext', () => {
6767

6868
expect(() => context.run('number + 1')).toThrow(new ReferenceError('number is not defined'));
6969
});
70-
});
7170

72-
describe('SealedExecutionContext', () => {
73-
it('prevents context modifications ', () => {
74-
const context = new ExecutionContext(null);
75-
const sealedContext = new SealedExecutionContext(context);
71+
describe('SealedExecutionContext', () => {
72+
it('prevents context modifications ', () => {
73+
const context = new ExecutionContext(null);
74+
const sealedContext = new SealedExecutionContext(context);
7675

77-
sealedContext.reset();
78-
sealedContext.addLocals({ local: true });
79-
expect(() => sealedContext.run('local')).toThrow(new ReferenceError('local is not defined'));
76+
sealedContext.reset();
77+
sealedContext.addLocals({ local: true });
78+
expect(() => sealedContext.run('local')).toThrow(new ReferenceError('local is not defined'));
79+
});
8080
});
8181
});
82+

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'reflect-metadata';
22

3-
export { Application } from './application';
4-
export { Bootstrap, BootstrapOptions } from './bootstrap';
3+
export { Bootstrap, BootstrapOptions, Application } from './bootstrap';
54

65
export {
76
Change,

0 commit comments

Comments
 (0)