| Conditions | 1 |
| Paths | 1 |
| Total Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import {expect} from 'chai'; |
||
| 4 | describe('store', () => { |
||
| 5 | |||
| 6 | beforeEach(() => { |
||
| 7 | store.reset(); |
||
| 8 | }); |
||
| 9 | |||
| 10 | describe('set', () => { |
||
| 11 | it('should be able to set a key/value', () => { |
||
| 12 | store.set('foo', 'bar'); |
||
| 13 | expect(store._store).to.have.property('foo', 'bar'); |
||
| 14 | }); |
||
| 15 | |||
| 16 | it('should be able to set a multi level key', () => { |
||
| 17 | store.set('foo.bar', 42); |
||
| 18 | expect(store._store).to.have.property('foo'); |
||
| 19 | expect(store._store.foo).to.have.property('bar', 42); |
||
| 20 | }); |
||
| 21 | |||
| 22 | it('should be able to set a multi level key', () => { |
||
| 23 | store.set('foo.bar.deep', 42); |
||
| 24 | expect(store._store).to.have.property('foo'); |
||
| 25 | expect(store._store.foo).to.have.property('bar'); |
||
| 26 | expect(store._store.foo.bar).to.have.property('deep', 42); |
||
| 27 | }); |
||
| 28 | |||
| 29 | it('should be able to set multiple multi level key', () => { |
||
| 30 | store.set('foo.bar.deep', 42); |
||
| 31 | store.set('foo.baz.deep', 43); |
||
| 32 | expect(store._store).to.have.property('foo'); |
||
| 33 | expect(store._store.foo).to.have.property('bar'); |
||
| 34 | expect(store._store.foo).to.have.property('baz'); |
||
| 35 | expect(store._store.foo.bar).to.have.property('deep', 42); |
||
| 36 | expect(store._store.foo.baz).to.have.property('deep', 43); |
||
| 37 | }); |
||
| 38 | |||
| 39 | }); |
||
| 40 | |||
| 41 | describe('get', () => { |
||
| 42 | it('should be able to recover a value from the store', () => { |
||
| 43 | store._store = {'foo': 'bar'}; |
||
| 44 | expect(store.get('foo')).to.equal('bar'); |
||
| 45 | }); |
||
| 46 | |||
| 47 | it('should be able to read a multi level key from the store', () => { |
||
| 48 | store._store = {'foo': {'bar': {'deep': 42}}}; |
||
| 49 | |||
| 50 | let value = store.get('foo.bar'); |
||
| 51 | expect(value).to.have.property('deep', 42); |
||
| 52 | }); |
||
| 53 | |||
| 54 | it('should be able to read a multi level key from the store', () => { |
||
| 55 | store._store = {'foo': {'bar': {'deep': 42}}}; |
||
| 56 | expect(store.get('foo.bar.deep')).to.equal(42); |
||
| 57 | }); |
||
| 58 | |||
| 59 | it('should return the whole store if no key is given', () => { |
||
| 60 | store._store = {'foo': 'bar', 'foo2': 42}; |
||
| 61 | let value = store.get(); |
||
| 62 | expect(value).to.have.property('foo', 'bar'); |
||
| 63 | expect(value).to.have.property('foo2', 42); |
||
| 64 | }); |
||
| 65 | |||
| 66 | it('should return the default value if the key is undefined', () => { |
||
| 67 | store._store = {}; |
||
| 68 | expect(store.get('foo.bar', 42)).to.equal(42); |
||
| 69 | }); |
||
| 70 | }); |
||
| 71 | |||
| 72 | describe('reset', () => { |
||
| 73 | it('should empty the store', () => { |
||
| 74 | store._store = {'foo': 'bar'}; |
||
| 75 | store.reset(); |
||
| 76 | expect(store._store).not.to.have.property('foo'); |
||
| 77 | }); |
||
| 78 | }); |
||
| 79 | |||
| 80 | }); |
||
| 81 |