Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
test(Condition): additional condition tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thore3 committed Oct 20, 2017
1 parent b196aab commit 3eac98b
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions tests/conditions/Condition.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@
const Condition = require('../../src/conditions/Condition');

describe('Condition', () => {
const passed = Condition.of(() => ({
passed: true,
reason: ['this is a passed condition']
}))

const failed = Condition.of(() => ({
passed: false,
reason: ['this is a failed condition']
}))
const passed = Condition.of(() => ({ passed: true, reason: ['cond_passing'] }))
const failed = Condition.of(() => ({ passed: false, reason: ['cond_failing'] }))

describe('#empty', () => {
it('should create an empty condition', () => {
Expand All @@ -21,51 +14,67 @@ describe('Condition', () => {

describe('#of', () => {
it('should create a new condition', () => {
let c = Condition.of(() => ({
passed: false,
reason: ['just because']
}))
expect(c.test()).toEqual({ passed: false, reason: ['just because'] })
let c = Condition.of(() => ({ passed: false, reason: ['reason'] }))
expect(c.test()).toEqual({ passed: false, reason: ['reason'] })
})
})

describe('.and', () => {
it('should create a composite condition', () => {
let c = Condition.empty().and(failed)
expect(c.test()).toEqual({ passed: false, reason: ['this is a failed condition'] })
expect(c.test()).toEqual(failed.test())
})

it('should include all reasons for a passing condition', () => {
let c = passed.and(passed)
expect(c.test()).toEqual({ passed: true, reason: ['cond_passing', 'cond_passing'] })
})

it('should short-circuit on a failed first condition', () => {
let spy = jasmine.createSpy('test cond')
let c = failed.and(Condition.of(() => spy()))
expect(c.test()).toEqual(failed.test())
expect(spy).not.toHaveBeenCalled()
})
})

describe('.andNot', () => {
it('should create a composite negated condition', () => {
let c = Condition.empty().andNot(passed)
expect(c.test()).toEqual({ passed: false, reason: ['this is a passed condition'] })
expect(c.test()).toEqual({ passed: false, reason: ['cond_passing'] })
})
})

describe('.or', () => {
it('should create a passing condition if one branch passes', () => {
let c1 = passed.or(failed)
let c2 = failed.or(passed)
expect(c1.test()).toEqual({ passed: true, reason: ['this is a passed condition'] })
expect(c2.test()).toEqual({ passed: true, reason: ['this is a passed condition'] })
expect(c1.test()).toEqual(passed.test())
expect(c2.test()).toEqual(passed.test())
})

it('should create a failing condition if both branches fail', () => {
let c = failed.or(failed)
expect(c.test()).toEqual({ passed: false, reason: ['this is a failed condition'] })
expect(c.test()).toEqual(failed.test())
})

it('should short-circuit on a passed first condition', () => {
let spy = jasmine.createSpy('test cond')
let c = passed.or(Condition.of(() => spy()))
expect(c.test()).toEqual(passed.test())
expect(spy).not.toHaveBeenCalled()
})
})

describe('.negated', () => {
it('should negate a passing condition', () => {
let c = passed.negated()
expect(c.test()).toEqual({ passed: false, reason: ['this is a passed condition'] })
expect(c.test()).toEqual({ passed: false, reason: ['cond_passing'] })
})

it('should negate a failing condition', () => {
let c = failed.negated()
expect(c.test()).toEqual({ passed: true, reason: ['this is a failed condition'] })
expect(c.test()).toEqual({ passed: true, reason: ['cond_failing'] })
})
})
})

1 comment on commit 3eac98b

@Artic2019
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ь

Please sign in to comment.