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

fix(core): detach SegmentedBar/TabView items from a previous parent before adopting them by edusperoni · Pull Request #11353 · NativeScript/NativeScript · GitHub

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

Filter by extension

Filter by extension .ts  (4) 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
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 @@ -274,6 +274,39 @@ export function test_SettingNumberAsTitleFromXML_DoesNotThrow() {
});
}

export function test_SettingTheSameItemsToAnotherSegmentedBar_DoesNotThrow() {
const items = _createItems(3);
const firstBar = _createSegmentedBar();

buildUIAndRunTest(firstBar, function (views: Array<View>) {
firstBar.items = items;

const secondBar = _createSegmentedBar();
secondBar.items = items;

items.forEach((item, i) => TKUnit.assertEqual(item.parent, secondBar, `Item ${i} should be parented to the SegmentedBar it was last assigned to.`));
});
}

export function test_SettingTheItemsOfATornDownSegmentedBarToANewOne_DoesNotThrow() {
const items = _createItems(3);
const firstBar = _createSegmentedBar();

buildUIAndRunTest(firstBar, function (views: Array<View>) {
firstBar.items = items;
});

// The test above leaves the page without content, so firstBar is torn down while its items still point at it.
const secondBar = _createSegmentedBar();

buildUIAndRunTest(secondBar, function (views: Array<View>) {
secondBar.items = items;

items.forEach((item, i) => TKUnit.assertEqual(item.parent, secondBar, `Item ${i} should be parented to the SegmentedBar it was last assigned to.`));
TKUnit.assertEqual(segmentedBarTestsNative.getNativeItemsCount(secondBar), items.length, 'Native items should be created for the SegmentedBar the items were re-assigned to.');
});
}

/*export function testBackgroundColorUpdatedAfterItemSelected() {
let segmentedBar = new segmentedBarModule.SegmentedBar();
let item1 = new segmentedBarModule.SegmentedBarItem();
Expand Down
30 changes: 30 additions & 0 deletions apps/automated/src/ui/tab-view/tab-view-tests.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 @@ -213,6 +213,36 @@ export class TabViewTest extends UITest<tabViewModule.TabView> {
}, 'Binding TabView to a TabViewItem with null view should throw.');
};

public test_SettingTheSameItemsToAnotherTabView_DoesNotThrow = function () {
var tabView = this.testView;
var items = this._createItems(2);
tabView.items = items;
this.waitUntilTestElementIsLoaded();

var secondTabView = new tabViewModule.TabView();
secondTabView.items = items;

items.forEach((item, i) => TKUnit.assertEqual(item.parent, secondTabView, `Item ${i} should be parented to the TabView it was last assigned to.`));
};

public test_SettingTheItemsOfATornDownTabViewToANewOne_DoesNotThrow = function () {
var tabView = this.testView;
var items = this._createItems(2);
tabView.items = items;
this.waitUntilTestElementIsLoaded();

// Detaching the TabView tears it down, but its items still point at it.
this.testPage.content = null;

var secondTabView = new tabViewModule.TabView();
secondTabView.items = items;
this.testPage.content = secondTabView;
TKUnit.waitUntilReady(() => secondTabView.isLoaded, 1);

items.forEach((item, i) => TKUnit.assertEqual(item.parent, secondTabView, `Item ${i} should be parented to the TabView it was last assigned to.`));
TKUnit.assertEqual(tabViewTestsNative.getNativeTabCount(secondTabView), items.length, 'Native tabs should be created for the TabView the items were re-assigned to.');
};

public testWhenSelectingATabNativelySelectedIndexIsUpdatedProperly = function () {
var tabView = this.testView;
tabView.items = this._createItems(2);
Expand Down
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 @@ -84,7 +84,13 @@ export abstract class SegmentedBarBase extends View implements SegmentedBarDefin

if (newItems) {
for (let i = 0, count = newItems.length; i < count; i++) {
this._addView(newItems[i]);
const item = newItems[i];
// A view can only have one parent, and _addView throws if it already has one.
if (item.parent && item.parent !== this) {
item.parent._removeView(item);
}

this._addView(item);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/ui/tab-view/tab-view-common.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 @@ -207,6 +207,11 @@ export class TabViewBase extends View implements TabViewDefinition, AddChildFrom
throw new Error(`TabViewItem must have a view.`);
}

// A view can only have one parent, and _addView throws if it already has one.
if (item.parent && item.parent !== this) {
item.parent._removeView(item);
}

this._addView(item);
});
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL