mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-12 11:40:33 +08:00
[Navigator] A method subtract to compute the diff between stack mutation.
Summary: When mutation of a stack happens, we'd like to compute the diff of the stacks (before and after) so that we can know which routes are removed in the new stack. This diff adds a new method `substract` which does what we need.
This commit is contained in:
@@ -31,6 +31,10 @@ jest
|
||||
|
||||
var NavigationRouteStack = require('NavigationRouteStack');
|
||||
|
||||
function assetStringNotEmpty(str) {
|
||||
expect(!!str && typeof str === 'string').toBe(true);
|
||||
}
|
||||
|
||||
describe('NavigationRouteStack:', () => {
|
||||
// Different types of routes.
|
||||
var ROUTES = [
|
||||
@@ -341,4 +345,62 @@ describe('NavigationRouteStack:', () => {
|
||||
['b', 1, true],
|
||||
]);
|
||||
});
|
||||
|
||||
// Diff
|
||||
it('subtracts stack', () => {
|
||||
var stack1 = new NavigationRouteStack(2, ['a', 'b', 'c']);
|
||||
var stack2 = stack1.pop().pop().push('x').push('y');
|
||||
|
||||
var diff = stack1.subtract(stack2);
|
||||
|
||||
var result = diff.toJS().map((record) => {
|
||||
assetStringNotEmpty(record.key);
|
||||
return {
|
||||
index: record.index,
|
||||
route: record.route,
|
||||
};
|
||||
});
|
||||
|
||||
// route `b` and `c` are no longer in the stack.
|
||||
expect(result).toEqual([
|
||||
{
|
||||
index: 1,
|
||||
route: 'b',
|
||||
},
|
||||
{
|
||||
index: 2,
|
||||
route: 'c',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('only subtracts the derived stack', () => {
|
||||
var stack1 = new NavigationRouteStack(2, ['a', 'b', 'c']);
|
||||
var stack2 = new NavigationRouteStack(0, ['a']);
|
||||
var diff = stack1.subtract(stack2);
|
||||
|
||||
var result = diff.toJS().map((record) => {
|
||||
assetStringNotEmpty(record.key);
|
||||
return {
|
||||
index: record.index,
|
||||
route: record.route,
|
||||
};
|
||||
});
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
index: 0,
|
||||
route: 'a',
|
||||
},
|
||||
{
|
||||
index: 1,
|
||||
route: 'b',
|
||||
},
|
||||
{
|
||||
index: 2,
|
||||
route: 'c',
|
||||
},
|
||||
]);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user