FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(core): reset inherited children when an InheritedProperty is unset by edusperoni · Pull Request #11413 · NativeScript/NativeScript · GitHub

Repository navigation

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion packages/core/ui/core/properties/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ export class InheritedProperty<T extends ViewBase, U> extends Property<T, U> imp
const childValueSource = child[sourceKey] || ValueSource.Default;
if (reset) {
if (childValueSource === ValueSource.Inherited) {
setFunc.call(child, unsetValue);
setInheritedValue.call(child, unsetValue);
}
} else {
if (childValueSource <= ValueSource.Inherited) {
Expand Down
141 changes: 141 additions & 0 deletions packages/core/ui/core/properties/inherited-property.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { describe, it, expect } from 'vitest';

import { InheritedProperty, unsetValue } from './index';
import { StackLayout } from '../../layouts/stack-layout';
import { Label } from '../../label';

// Mirrors the private `ValueSource` enum in ./index.
const ValueSource = {
Default: 0,
Inherited: 1,
Css: 2,
Local: 3,
};

class TestView extends StackLayout {
public testInherited: string;

public createNativeView(): Object {
return {};
}
}

const testInheritedProperty = new InheritedProperty<TestView, string>({
name: 'testInherited',
defaultValue: undefined,
});
testInheritedProperty.register(TestView);

function valueSourceOf(view: any): number {
return view[testInheritedProperty.sourceKey] || ValueSource.Default;
}

function tree(): { root: TestView; child: TestView; grandChild: TestView } {
const root = new TestView();
const child = new TestView();
const grandChild = new TestView();

root.addChild(child);
child.addChild(grandChild);

return { root, child, grandChild };
}

function recordChanges(view: any, eventName: string): unknown[] {
const values: unknown[] = [];
view.on(eventName, (data: any) => values.push(data.value));

return values;
}

describe('InheritedProperty', () => {
it('propagates a value down the tree', () => {
const { root, child, grandChild } = tree();

root.testInherited = 'a';

expect(child.testInherited).toBe('a');
expect(grandChild.testInherited).toBe('a');
expect(valueSourceOf(root)).toBe(ValueSource.Local);
expect(valueSourceOf(child)).toBe(ValueSource.Inherited);
expect(valueSourceOf(grandChild)).toBe(ValueSource.Inherited);
});

it('resets inheriting descendants when the value is unset', () => {
const { root, child, grandChild } = tree();
root.testInherited = 'a';

const childChanges = recordChanges(child, 'testInheritedChange');
const grandChildChanges = recordChanges(grandChild, 'testInheritedChange');

root.testInherited = unsetValue;

expect(root.testInherited).toBeUndefined();
expect(child.testInherited).toBeUndefined();
expect(grandChild.testInherited).toBeUndefined();
expect(valueSourceOf(root)).toBe(ValueSource.Default);
expect(valueSourceOf(child)).toBe(ValueSource.Default);
expect(valueSourceOf(grandChild)).toBe(ValueSource.Default);
expect(childChanges).toEqual([undefined]);
expect(grandChildChanges).toEqual([undefined]);
});

it("resets inheriting descendants when the value is set to 'initial'", () => {
const { root, child, grandChild } = tree();
root.testInherited = 'a';

root.testInherited = <any>'initial';

expect(child.testInherited).toBeUndefined();
expect(grandChild.testInherited).toBeUndefined();
});

it('leaves a descendant that holds a local value untouched', () => {
const { root, child, grandChild } = tree();
root.testInherited = 'a';
child.testInherited = 'local';

root.testInherited = unsetValue;

expect(child.testInherited).toBe('local');
expect(valueSourceOf(child)).toBe(ValueSource.Local);
expect(grandChild.testInherited).toBe('local');
expect(valueSourceOf(grandChild)).toBe(ValueSource.Inherited);
});

it('propagates again after a reset', () => {
const { root, child, grandChild } = tree();
root.testInherited = 'a';
root.testInherited = unsetValue;

root.testInherited = 'b';

expect(child.testInherited).toBe('b');
expect(grandChild.testInherited).toBe('b');
expect(valueSourceOf(grandChild)).toBe(ValueSource.Inherited);
});
});

describe('bindingContext', () => {
it('is reset on descendants when the root unsets it', () => {
const root = new StackLayout();
const container = new StackLayout();
const label = new Label();
root.addChild(container);
container.addChild(label);

const context = { name: 'a' };
root.bindingContext = context;
expect(container.bindingContext).toBe(context);
expect(label.bindingContext).toBe(context);

const labelChanges = recordChanges(label, 'bindingContextChange');

root.bindingContext = unsetValue;

expect(root.bindingContext).toBeUndefined();
expect(container.bindingContext).toBeUndefined();
expect(label.bindingContext).toBeUndefined();
expect(labelChanges).toEqual([undefined]);
});
});
Loading

Back | FazBrowse Home | New Git URL