-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from airbnb/refactor-template-reuse
Refactor template reuse & fix `getSubtreeId`
- Loading branch information
Showing
20 changed files
with
266 additions
and
241 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
50 changes: 50 additions & 0 deletions
50
packages/core/src/reconciler/__tests__/getSubtreeId.noSubtree.test.tsx
This file contains 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,50 @@ | ||
import { ElementInstance } from '../instance'; | ||
import { Container } from '../../container'; | ||
import { GOJI_VIRTUAL_ROOT, TYPE_SUBTREE } from '../../constants'; | ||
import { TestingAdaptorInstance } from '../../__tests__/helpers/adaptor'; | ||
|
||
jest.mock('../../components/subtree', () => ({ | ||
useSubtree: false, | ||
subtreeMaxDepthFromConfig: 5, | ||
})); | ||
|
||
beforeAll(() => { | ||
// @ts-expect-error | ||
process.env.GOJI_WRAPPED_COMPONENTS = []; | ||
}); | ||
const view = () => new ElementInstance('view', {}, [], new Container(new TestingAdaptorInstance())); | ||
const subtree = () => | ||
new ElementInstance(TYPE_SUBTREE, {}, [], new Container(new TestingAdaptorInstance())); | ||
/** | ||
* linkElements | ||
* call `setParent` and `children.push` for given elements | ||
* [a, b, c] => a <- b <- c | ||
*/ | ||
const linkElements = (elements: Array<ElementInstance>) => { | ||
const mockContainer = new Container(new TestingAdaptorInstance()); | ||
const mockRoot = new ElementInstance(GOJI_VIRTUAL_ROOT, {}, [], mockContainer); | ||
elements[0].setParent(mockRoot); | ||
for (let i = 0; i < elements.length - 1; i += 1) { | ||
elements[i].appendChild(elements[i + 1]); | ||
} | ||
}; | ||
|
||
describe('no subtree', () => { | ||
test('subtree id should be always undefined', () => { | ||
let leaf: ElementInstance; | ||
const elements = [ | ||
view(), | ||
view(), | ||
subtree(), | ||
view(), | ||
view(), | ||
view(), | ||
view(), | ||
view(), | ||
view(), | ||
(leaf = view()), | ||
]; | ||
linkElements(elements); | ||
expect(leaf.getSubtreeId()).toBe(undefined); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
packages/core/src/reconciler/__tests__/getSubtreeId.test.tsx
This file contains 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,74 @@ | ||
import { ElementInstance } from '../instance'; | ||
import { Container } from '../../container'; | ||
import { GOJI_VIRTUAL_ROOT, TYPE_SUBTREE } from '../../constants'; | ||
import { TestingAdaptorInstance } from '../../__tests__/helpers/adaptor'; | ||
|
||
jest.mock('../../components/subtree', () => ({ | ||
useSubtree: true, | ||
subtreeMaxDepthFromConfig: 5, | ||
})); | ||
|
||
beforeAll(() => { | ||
// @ts-expect-error | ||
process.env.GOJI_WRAPPED_COMPONENTS = []; | ||
}); | ||
const view = () => new ElementInstance('view', {}, [], new Container(new TestingAdaptorInstance())); | ||
const subtree = () => | ||
new ElementInstance(TYPE_SUBTREE, {}, [], new Container(new TestingAdaptorInstance())); | ||
/** | ||
* linkElements | ||
* call `setParent` and `children.push` for given elements | ||
* [a, b, c] => a <- b <- c | ||
*/ | ||
const linkElements = (elements: Array<ElementInstance>) => { | ||
const mockContainer = new Container(new TestingAdaptorInstance()); | ||
const mockRoot = new ElementInstance(GOJI_VIRTUAL_ROOT, {}, [], mockContainer); | ||
elements[0].setParent(mockRoot); | ||
for (let i = 0; i < elements.length - 1; i += 1) { | ||
elements[i].appendChild(elements[i + 1]); | ||
} | ||
}; | ||
|
||
describe('use subtree', () => { | ||
test('container works', () => { | ||
let leaf: ElementInstance; | ||
const elements = [view(), view(), (leaf = view())]; | ||
linkElements(elements); | ||
expect(leaf.getSubtreeId()).toBe(undefined); | ||
}); | ||
|
||
test('auto subtree works', () => { | ||
let leaf: ElementInstance; | ||
let sub: ElementInstance; | ||
const elements = [view(), view(), view(), view(), (sub = view()), view(), (leaf = view())]; | ||
linkElements(elements); | ||
expect(leaf.getSubtreeId()).toBe(sub.id); | ||
}); | ||
|
||
test('manual subtree works', () => { | ||
let leaf: ElementInstance; | ||
let sub: ElementInstance; | ||
const elements = [view(), view(), (sub = subtree()), view(), (leaf = view())]; | ||
linkElements(elements); | ||
expect(leaf.getSubtreeId()).toBe(sub.id); | ||
}); | ||
|
||
test('auto subtree inside manual subtree works', () => { | ||
let leaf: ElementInstance; | ||
let sub: ElementInstance; | ||
const elements = [ | ||
view(), | ||
view(), | ||
subtree(), | ||
view(), | ||
view(), | ||
view(), | ||
view(), | ||
(sub = view()), | ||
view(), | ||
(leaf = view()), | ||
]; | ||
linkElements(elements); | ||
expect(leaf.getSubtreeId()).toBe(sub.id); | ||
}); | ||
}); |
This file contains 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
Oops, something went wrong.