Skip to content
This repository has been archived by the owner on Jan 15, 2020. It is now read-only.

Commit

Permalink
Improve code presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sveyret committed Jun 23, 2018
1 parent 8c5f50d commit 4c1917a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 113 deletions.
139 changes: 32 additions & 107 deletions src/loadable.spec.ts
Expand Up @@ -14,8 +14,7 @@ interface Person {
lastName: string
}

const MOTO: string =
'Timigu min, kaj mi obeos honte ; konvinku min, kaj mi obeos volonte'
const MOTO: string = 'Timigu min, kaj mi obeos honte ; konvinku min, kaj mi obeos volonte'

class ExampleClass {
@loadable() public readonly persons: Person[] = []
Expand Down Expand Up @@ -131,10 +130,7 @@ describe('@loadable', function() {
})
)
})
expect(
isLoading(example.persons),
'Persons should not be loading'
).to.be.false
expect(isLoading(example.persons), 'Persons should not be loading').to.be.false
expect(isLoading(example.boss), 'Boss should not be loading').to.be.false
expect(isLoading(example.moto), 'Moto should not be loading').to.be.false
expect(example.persons, 'Unexpected value for persons').to.have.lengthOf(0)
Expand All @@ -148,75 +144,38 @@ describe('@loadable', function() {

it('must throw an error if applying to a non modifiable property', function() {
Object.defineProperty(example, 'non-modifiable', { configurable: false })
expect(() => loadable(example, 'non-modifiable')).to.throw(
/cannot convert non-modifiable to loadable/i
)
expect(() => loadable(example, 'non-modifiable')).to.throw(/cannot convert non-modifiable to loadable/i)
})

it('must throw an error if non applied on property given to @load', function() {
const descriptor: any = Object.getOwnPropertyDescriptor(example, 'loadMoto')
expect(() => load('counter')(example, 'loadMoto', descriptor)).to.throw(
/is not a @loadable/i
)
expect(() => load('counter')(example, 'loadMoto', descriptor)).to.throw(/is not a @loadable/i)
})

it('must throw an error if non existing property given to @load', function() {
const descriptor: any = Object.getOwnPropertyDescriptor(example, 'loadMoto')
expect(() => load('unknown')(example, 'loadMoto', descriptor)).to.throw(
/is not a @loadable/i
)
expect(() => load('unknown')(example, 'loadMoto', descriptor)).to.throw(/is not a @loadable/i)
})

describe('@load(boolean)', function() {
it('must indicate when loading', async function() {
let promise
promise = example.loadPersons()
expect(
example.persons,
'Persons should not be loaded yet'
).to.have.lengthOf(0)
expect(example.persons, 'Persons should not be loaded yet').to.have.lengthOf(0)
expect(isLoading(example.persons), 'Persons should be loading').to.be.true
expect(
counters.persons,
'Update state of persons should have been detected'
).to.equal(1)
expect(counters.persons, 'Update state of persons should have been detected').to.equal(1)
await promise
expect(
example.persons,
'All persons should now be loaded'
).to.have.lengthOf(FAMOUS.length)
expect(
isLoading(example.persons),
'Persons should not be loading anymore'
).to.be.false
expect(
counters.persons,
'Update state of persons should have been detected'
).to.equal(2)
expect(example.persons, 'All persons should now be loaded').to.have.lengthOf(FAMOUS.length)
expect(isLoading(example.persons), 'Persons should not be loading anymore').to.be.false
expect(counters.persons, 'Update state of persons should have been detected').to.equal(2)
promise = example.loadPersons()
expect(
example.persons,
'Persons should have kept old values'
).to.have.lengthOf(FAMOUS.length)
expect(isLoading(example.persons), 'Persons should be loading once more')
.to.be.true
expect(
counters.persons,
'Update state of persons should have been detected'
).to.equal(3)
expect(example.persons, 'Persons should have kept old values').to.have.lengthOf(FAMOUS.length)
expect(isLoading(example.persons), 'Persons should be loading once more').to.be.true
expect(counters.persons, 'Update state of persons should have been detected').to.equal(3)
await promise
expect(
example.persons,
'Persons should be updated with emptyness'
).to.have.lengthOf(0)
expect(
isLoading(example.persons),
'New persons loading should be finished'
).to.be.false
expect(
counters.persons,
'Update state of persons should have been detected'
).to.equal(4)
expect(example.persons, 'Persons should be updated with emptyness').to.have.lengthOf(0)
expect(isLoading(example.persons), 'New persons loading should be finished').to.be.false
expect(counters.persons, 'Update state of persons should have been detected').to.equal(4)
expect(counters.boss, 'Boss state should not have changed').to.equal(0)
expect(counters.moto, 'Moto state should not have changed').to.equal(0)
})
Expand All @@ -226,47 +185,22 @@ describe('@loadable', function() {
it('must indicate when loading', async function() {
let promise
promise = example.loadBoss()
expect(example.boss, 'Boss should have last value').to.deep.equal(
FAMOUS[0]
)
expect(example.boss, 'Boss should have last value').to.deep.equal(FAMOUS[0])
expect(isLoading(example.boss), 'Boss should be loading').to.be.true
expect(
counters.boss,
'Update state of boss should have been detected'
).to.equal(1)
expect(counters.boss, 'Update state of boss should have been detected').to.equal(1)
await promise
expect(example.boss, 'Boss should have loaded value').to.deep.equal(
FAMOUS[1]
)
expect(isLoading(example.boss), 'Boss should not be loading anymore').to
.be.false
expect(
counters.boss,
'Update state of boss should have been detected'
).to.equal(2)
expect(example.boss, 'Boss should have loaded value').to.deep.equal(FAMOUS[1])
expect(isLoading(example.boss), 'Boss should not be loading anymore').to.be.false
expect(counters.boss, 'Update state of boss should have been detected').to.equal(2)
promise = example.loadBoss()
expect(example.boss, 'Boss should still have loaded value').to.deep.equal(
FAMOUS[1]
)
expect(isLoading(example.boss), 'Boss should be loading once more').to.be
.true
expect(
counters.boss,
'Update state of boss should have been detected'
).to.equal(3)
expect(example.boss, 'Boss should still have loaded value').to.deep.equal(FAMOUS[1])
expect(isLoading(example.boss), 'Boss should be loading once more').to.be.true
expect(counters.boss, 'Update state of boss should have been detected').to.equal(3)
await promise
expect(example.boss, 'Boss should have new loaded value').to.deep.equal(
FAMOUS[2]
)
expect(isLoading(example.boss), 'New boss loading should be finished').to
.be.false
expect(
counters.boss,
'Update state of boss should have been detected'
).to.equal(4)
expect(counters.persons, 'Person state should not have changed').to.equal(
0
)
expect(example.boss, 'Boss should have new loaded value').to.deep.equal(FAMOUS[2])
expect(isLoading(example.boss), 'New boss loading should be finished').to.be.false
expect(counters.boss, 'Update state of boss should have been detected').to.equal(4)
expect(counters.persons, 'Person state should not have changed').to.equal(0)
expect(counters.moto, 'Moto state should not have changed').to.equal(0)
})
})
Expand All @@ -276,21 +210,12 @@ describe('@loadable', function() {
let promise
promise = example.loadMoto()
expect(isLoading(example.moto), 'Moto should be loading').to.be.true
expect(
counters.moto,
'Update state of moto should have been detected'
).to.equal(1)
expect(counters.moto, 'Update state of moto should have been detected').to.equal(1)
await promise
expect(example.moto, 'Unexpected moto loaded value').to.be.an('object')
expect(isLoading(example.moto), 'Moto should not be loading anymore').to
.be.false
expect(
counters.moto,
'Update state of moto should have been detected'
).to.equal(2)
expect(counters.persons, 'Person state should not have changed').to.equal(
0
)
expect(isLoading(example.moto), 'Moto should not be loading anymore').to.be.false
expect(counters.moto, 'Update state of moto should have been detected').to.equal(2)
expect(counters.persons, 'Person state should not have changed').to.equal(0)
expect(counters.boss, 'Boss state should not have changed').to.equal(0)
})
})
Expand Down
8 changes: 2 additions & 6 deletions src/loadable.ts
Expand Up @@ -50,10 +50,7 @@ const $loadable = Symbol('Loadable data')
export function loadable(): PropertyDecorator
// tslint:disable-next-line:ban-types (This is taken from PropertyDecorator signature)
export function loadable(target: Object, propertyKey: string | symbol): void
export function loadable(
target?: any,
propertyKey?: string | symbol
): PropertyDecorator | void {
export function loadable(target?: any, propertyKey?: string | symbol): PropertyDecorator | void {
// Called as a factory
if (propertyKey === undefined) {
return loadable
Expand Down Expand Up @@ -111,8 +108,7 @@ export function load(
} else {
return (target: any, _key, descriptor) => {
if (target[$loadable] && target[$loadable][propertyKey]) {
const loadableProperty: LoadableProperty =
target[$loadable][propertyKey]
const loadableProperty: LoadableProperty = target[$loadable][propertyKey]
const originalMethod: any = descriptor.value!
descriptor.value = function(this: any, ...args: any[]) {
loadableProperty.updateLoading(statusExtractor(...args))
Expand Down

0 comments on commit 4c1917a

Please sign in to comment.