| Conditions | 1 |
| Paths | 1 |
| Total Lines | 108 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 sinon from 'sinon'; |
||
| 5 | describe('ServersService', () => { |
||
| 6 | const servers = { |
||
| 7 | abc123: { id: 'abc123' }, |
||
| 8 | def456: { id: 'def456' }, |
||
| 9 | }; |
||
| 10 | const createStorageMock = (returnValue) => ({ |
||
| 11 | set: sinon.fake(), |
||
| 12 | get: sinon.fake.returns(returnValue), |
||
| 13 | }); |
||
| 14 | |||
| 15 | describe('listServers', () => { |
||
| 16 | it('returns an empty object when servers are not found in storage', () => { |
||
| 17 | const storageMock = createStorageMock(); |
||
| 18 | const service = new ServersService(storageMock); |
||
| 19 | |||
| 20 | const result = service.listServers(); |
||
| 21 | |||
| 22 | expect(result).toEqual({}); |
||
| 23 | expect(storageMock.get.callCount).toEqual(1); |
||
| 24 | expect(storageMock.set.callCount).toEqual(0); |
||
| 25 | }); |
||
| 26 | |||
| 27 | it('returns value from storage when found', () => { |
||
| 28 | const storageMock = createStorageMock(servers); |
||
| 29 | const service = new ServersService(storageMock); |
||
| 30 | |||
| 31 | const result = service.listServers(); |
||
| 32 | |||
| 33 | expect(result).toEqual(servers); |
||
| 34 | expect(storageMock.get.callCount).toEqual(1); |
||
| 35 | expect(storageMock.set.callCount).toEqual(0); |
||
| 36 | }); |
||
| 37 | }); |
||
| 38 | |||
| 39 | describe('findServerById', () => { |
||
| 40 | it('returns undefined when requested server is not found', () => { |
||
| 41 | const storageMock = createStorageMock(servers); |
||
| 42 | const service = new ServersService(storageMock); |
||
| 43 | |||
| 44 | const result = service.findServerById('ghi789'); |
||
| 45 | |||
| 46 | expect(result).toBeUndefined(); |
||
| 47 | expect(storageMock.get.callCount).toEqual(1); |
||
| 48 | expect(storageMock.set.callCount).toEqual(0); |
||
| 49 | }); |
||
| 50 | |||
| 51 | it('returns server from list when found', () => { |
||
| 52 | const storageMock = createStorageMock(servers); |
||
| 53 | const service = new ServersService(storageMock); |
||
| 54 | |||
| 55 | const result = service.findServerById('abc123'); |
||
| 56 | |||
| 57 | expect(result).toEqual({ id: 'abc123' }); |
||
| 58 | expect(storageMock.get.callCount).toEqual(1); |
||
| 59 | expect(storageMock.set.callCount).toEqual(0); |
||
| 60 | }); |
||
| 61 | }); |
||
| 62 | |||
| 63 | describe('createServer', () => { |
||
| 64 | it('adds one server to the list', () => { |
||
| 65 | const storageMock = createStorageMock(servers); |
||
| 66 | const service = new ServersService(storageMock); |
||
| 67 | |||
| 68 | service.createServer({ id: 'ghi789' }); |
||
| 69 | |||
| 70 | expect(storageMock.get.callCount).toEqual(1); |
||
| 71 | expect(storageMock.set.callCount).toEqual(1); |
||
| 72 | expect(last(storageMock.set.lastCall.args)).toEqual({ |
||
| 73 | abc123: { id: 'abc123' }, |
||
| 74 | def456: { id: 'def456' }, |
||
| 75 | ghi789: { id: 'ghi789' }, |
||
| 76 | }); |
||
| 77 | }); |
||
| 78 | }); |
||
| 79 | |||
| 80 | describe('createServers', () => { |
||
| 81 | it('adds multiple servers to the list', () => { |
||
| 82 | const storageMock = createStorageMock(servers); |
||
| 83 | const service = new ServersService(storageMock); |
||
| 84 | |||
| 85 | service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]); |
||
| 86 | |||
| 87 | expect(storageMock.get.callCount).toEqual(1); |
||
| 88 | expect(storageMock.set.callCount).toEqual(1); |
||
| 89 | expect(last(storageMock.set.lastCall.args)).toEqual({ |
||
| 90 | abc123: { id: 'abc123' }, |
||
| 91 | def456: { id: 'def456' }, |
||
| 92 | ghi789: { id: 'ghi789' }, |
||
| 93 | jkl123: { id: 'jkl123' }, |
||
| 94 | }); |
||
| 95 | }); |
||
| 96 | }); |
||
| 97 | |||
| 98 | describe('deleteServer', () => { |
||
| 99 | it('removes one server from the list', () => { |
||
| 100 | const storageMock = createStorageMock(servers); |
||
| 101 | const service = new ServersService(storageMock); |
||
| 102 | |||
| 103 | service.deleteServer({ id: 'abc123' }); |
||
| 104 | |||
| 105 | expect(storageMock.get.callCount).toEqual(1); |
||
| 106 | expect(storageMock.set.callCount).toEqual(1); |
||
| 107 | expect(last(storageMock.set.lastCall.args)).toEqual({ |
||
| 108 | def456: { id: 'def456' }, |
||
| 109 | }); |
||
| 110 | }); |
||
| 111 | }); |
||
| 112 | }); |
||
| 113 |