Skip to content

fix(rx-dynamic-component): update not initialized #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"nx": "if [ -z $CI ]; then NG_PERSISTENT_BUILD_CACHE=1; fi && node --max_old_space_size=4096 ./node_modules/.bin/nx",
"start": "yarn nx serve",
"build": "yarn affected:build --all --parallel",
"test": "yarn affected:test --all --parallel",
Expand Down
28 changes: 26 additions & 2 deletions packages/update/src/lib/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ class Store {

readonly counter2$ = new BehaviorSubject<number>(0);

readonly notDefined$ = new BehaviorSubject<number | undefined>(undefined);

readonly setCounter = (value: number) => this.counter$.next(value);

readonly setCounterTwo = (value: number) => this.counter2$.next(value);

readonly setNotDefined = (value: number) => this.notDefined$.next(value);
}

@Component({
Expand All @@ -30,9 +34,11 @@ class Store {
providers: [Store],
})
class TestComponent {
@Input() @Update(Store) counter = 0;
@Input() counter = 0;

@Input() @Update(Store, 'setCounterTwo') counterTwo = 0;
@Input() counterTwo = 0;

@Input() @Update(Store) notDefined?: number;

readonly counter$ = this.store.counter$;

Expand Down Expand Up @@ -60,6 +66,8 @@ describe('Update', () => {
expect(store).toBeDefined();

expect(subscribeSpyTo(store.counter$).getLastValue()).toEqual(0);

expect(component.counter).toBe(0);
});

it('should update the store value', () => {
Expand All @@ -68,6 +76,8 @@ describe('Update', () => {
fixture.detectChanges();

expect(subscribeSpyTo(store.counter$).getLastValue()).toEqual(1);

expect(component.counter).toBe(1);
});

it('should update the store value with renamed setter', () => {
Expand All @@ -76,5 +86,19 @@ describe('Update', () => {
fixture.detectChanges();

expect(subscribeSpyTo(store.counter2$).getLastValue()).toEqual(1);

expect(component.counterTwo).toBe(1);
});

it.only('should update a property when it is not initialized', () => {
expect(component.notDefined).not.toBeDefined();

component.notDefined = 1;

fixture.detectChanges();

expect(subscribeSpyTo(store.notDefined$).getLastValue()).toEqual(1);

expect(component.notDefined).toBe(1);
});
});
12 changes: 11 additions & 1 deletion packages/update/src/lib/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function Update<T, K extends keyof T>(
// eslint-disable-next-line @typescript-eslint/ban-types
injectMap.set(token, new WeakMap<object, T>()).get(token))!;

return (target: unknown, propertyName: string) => {
return (target: any, propertyName: string) => {
let service: T | null | undefined;

const updaterKey =
Expand Down Expand Up @@ -61,6 +61,16 @@ export function Update<T, K extends keyof T>(
get() {
return this[`_${propertyName}`];
},
enumerable: true,
});

console.log(target.prototype);

// const constructor = target.constructor;
//
// target.constructor = (...args: unknown[]) => {
// constructor(args);
// console.log('constructor');
// };
};
}