diff options
| author | Andrej Mihajlov <and@codeispoetry.ru> | 2017-06-19 19:16:16 +0300 |
|---|---|---|
| committer | Andrej Mihajlov <and@codeispoetry.ru> | 2017-06-21 12:27:05 +0300 |
| commit | 912141007dc076924bec8ed25b32d067e19a176b (patch) | |
| tree | 75bf7ddfdcaef08de17faa43639be8c7c1cf884f | |
| parent | 8ecd87d4df667469414291e81667b10680770f3e (diff) | |
| download | mullvadvpn-912141007dc076924bec8ed25b32d067e19a176b.tar.xz mullvadvpn-912141007dc076924bec8ed25b32d067e19a176b.zip | |
Replace TransitionRule.transitionDescriptor in favor of TransitionMatch
| -rw-r--r-- | app/lib/transition-rule.js | 73 | ||||
| -rw-r--r-- | test/transition-rule.spec.js | 38 |
2 files changed, 47 insertions, 64 deletions
diff --git a/app/lib/transition-rule.js b/app/lib/transition-rule.js index f0c7020870..a91ba4da66 100644 --- a/app/lib/transition-rule.js +++ b/app/lib/transition-rule.js @@ -10,65 +10,38 @@ export type TransitionFork = { backward: TransitionDescriptor }; -/** - * Transition rule - * - * @class TransitionRule - */ +export type TransitionMatch = { + direction: 'forward' | 'backward', + descriptor: TransitionDescriptor +}; + export default class TransitionRule { - from: ?string; - to: string; - fork: TransitionFork; - dir: 'forward' | 'backward' = 'forward'; + _from: ?string; + _to: string; + _fork: TransitionFork; - /** - * Creates an instance of TransitionRule. - * @param {string} from - source route to match against, pass null for any. - * @param {string} to - destination route to match against - * @param {TransitionFork} fork - transition - * - * @memberof TransitionRule - */ constructor(from: ?string, to: string, fork: TransitionFork) { - this.from = from; - this.to = to; - this.fork = fork; + this._from = from; + this._to = to; + this._fork = fork; } - /** - * Attempts to match the transition between routes A -> B and B -> A - * - * @param {string} [fromRoute] source route, pass null for any - * @param {string} toRoute - * @returns {boolean} true if matches, otherwise false - * - * @memberof TransitionRule - */ - match(fromRoute: ?string, toRoute: string): boolean { - if((!this.from || this.from === fromRoute) && this.to === toRoute) { - this.dir = 'forward'; - return true; + match(fromRoute: ?string, toRoute: string): ?TransitionMatch { + if((!this._from || this._from === fromRoute) && this._to === toRoute) { + return { + direction: 'forward', + descriptor: this._fork['forward'] + }; } - if((!this.from || this.from === toRoute) && this.to === fromRoute) { - this.dir = 'backward'; - return true; + if((!this._from || this._from === toRoute) && this._to === fromRoute) { + return { + direction: 'backward', + descriptor: this._fork['backward'] + }; } - return false; - } - - /** - * Returns transition descriptor. - * Make sure you run match() before to obtain the direction - * of transition before calling this method - * - * @returns {TransitionDescriptor} transitionDescriptor - * - * @memberof TransitionRule - */ - transitionDescriptor(): TransitionDescriptor { - return this.fork[this.dir]; + return null; } }
\ No newline at end of file diff --git a/test/transition-rule.spec.js b/test/transition-rule.spec.js index ba5ea15e64..647517ca5d 100644 --- a/test/transition-rule.spec.js +++ b/test/transition-rule.spec.js @@ -11,38 +11,48 @@ describe('TransitionRule', () => { it('should match wildcard rule', () => { const rule = new TransitionRule(null, '/route', testTransition); - expect(rule.match(null, '/route')).to.be.true; - expect(rule.transitionDescriptor().name).to.be.equal('forward'); + expect(rule.match(null, '/route')).to.deep.equal({ + direction: 'forward', + descriptor: { name: 'forward', duration: 0.25 } + }); - expect(rule.match('/somewhere', '/route')).to.be.true; - expect(rule.transitionDescriptor().name).to.be.equal('forward'); + expect(rule.match('/somewhere', '/route')).to.deep.equal({ + direction: 'forward', + descriptor: { name: 'forward', duration: 0.25 } + }); }); it('should match wildcard rule reversion', () => { const rule = new TransitionRule(null, '/route', testTransition); - expect(rule.match('/route', '/other')).to.be.true; - expect(rule.transitionDescriptor().name).to.be.equal('backward'); + expect(rule.match('/route', '/other')).to.deep.equal({ + direction: 'backward', + descriptor: { name: 'backward', duration: 0.25 } + }); }); it('should match exact rule', () => { const rule = new TransitionRule('/route1', '/route2', testTransition); - expect(rule.match('/other', '/route1')).to.be.false; - expect(rule.match('/other', '/route2')).to.be.false; + expect(rule.match('/other', '/route1')).to.be.null; + expect(rule.match('/other', '/route2')).to.be.null; - expect(rule.match('/route1', '/route2')).to.be.true; - expect(rule.transitionDescriptor().name).to.be.equal('forward'); + expect(rule.match('/route1', '/route2')).to.deep.equal({ + direction: 'forward', + descriptor: { name: 'forward', duration: 0.25 } + }); }); it('should match exact rule reversion', () => { const rule = new TransitionRule('/route1', '/route2', testTransition); - expect(rule.match('/route1', '/other')).to.be.false; - expect(rule.match('/route2', '/other')).to.be.false; + expect(rule.match('/route1', '/other')).to.be.null; + expect(rule.match('/route2', '/other')).to.be.null; - expect(rule.match('/route2', '/route1')).to.be.true; - expect(rule.transitionDescriptor().name).to.be.equal('backward'); + expect(rule.match('/route2', '/route1')).to.deep.equal({ + direction: 'backward', + descriptor: { name: 'backward', duration: 0.25 } + }); }); });
\ No newline at end of file |
