From 933f6f998dff35955b02b0357fbaad24be0f07cd Mon Sep 17 00:00:00 2001 From: Pavol Pitonak Date: Wed, 16 Jan 2019 17:55:12 +0100 Subject: [PATCH] fix(test): unit tests for SpaceService extended and refactored (#202) --- src/app/spaces/space.service.spec.ts | 485 ++++++++++++++++++--------- src/app/spaces/space.service.ts | 14 +- 2 files changed, 331 insertions(+), 168 deletions(-) diff --git a/src/app/spaces/space.service.spec.ts b/src/app/spaces/space.service.spec.ts index 41e92dd..32f1929 100644 --- a/src/app/spaces/space.service.spec.ts +++ b/src/app/spaces/space.service.spec.ts @@ -120,71 +120,110 @@ describe('Service: SpaceService', () => { } ]; + beforeEach(() => { + spaceService = new SpaceService(undefined, undefined, undefined, WIT_API_URL); + }); + describe('#getSpaces', () => { - it('should handle error when limit set to an invalid value', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); + describe('should handle error', () => { + it('when limit set to an invalid value (-2)', () => { + spaceService.getSpaces(-2).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); - spaceService.getSpaces(-2).subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); + it('when limit set to an invalid value (0)', () => { + spaceService.getSpaces(0).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when limit set to an invalid value (null)', () => { + spaceService.getSpaces(null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); }); - it('should delegate to function getSpacesDelegate with correct params', () => { - spaceService = new SpaceService(undefined, undefined, undefined, WIT_API_URL); - spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + describe('should delegate to the function getSpacesDelegate', () => { + it('when called with correct params (10)', () => { + spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); - spaceService.getSpaces(10).subscribe(() => { - expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); - expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain('page[limit]=10'); + spaceService.getSpaces(10).subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain(WIT_API_URL); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain('page[limit]=10'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called with correct params (undefined)', () => { + spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + + spaceService.getSpaces(undefined).subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain(WIT_API_URL); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain('page[limit]=20'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called without params', () => { + spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + + spaceService.getSpaces().subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain(WIT_API_URL); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain('page[limit]=20'); + validateReturnValues(returnValue, spaces); + }); }); }); }); describe('#getMoreSpaces', () => { it('should throw an error when nextLink is not defined', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); - - spaceService.getMoreSpaces().subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); - }); + spaceService.getMoreSpaces().subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); it('should delegate to function getSpacesDelegate with correct URL', () => { const nextLink = 'fake-next-link'; - spaceService = new SpaceService(undefined, undefined, undefined, undefined); spaceService['nextLink'] = nextLink; spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); - spaceService.getMoreSpaces().subscribe(() => { + spaceService.getMoreSpaces().subscribe((returnValue) => { expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(nextLink); + validateReturnValues(returnValue, spaces); }); }); }); describe('#getSpaceByName', () => { - it('should throw an error when userName is not defined', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); + describe('should throw an error', () => { + it('when userName is undefined', () => { + spaceService.getSpaceByName(undefined, 'myspace').subscribe( + failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); - spaceService.getSpaceByName(undefined, 'myspace').subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); + it('when userName is null', () => { + spaceService.getSpaceByName(null, 'myspace').subscribe( + failBecauseErrorIsExpected, verifyErrorNotEmpty); }); - }); - it('should throw an error when space name is not defined', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); + it('when userName is an empty string', () => { + spaceService.getSpaceByName('', 'myspace').subscribe( + failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); - spaceService.getSpaceByName('somebody', undefined).subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); + it('when space name is undefined', () => { + spaceService.getSpaceByName('somebody', undefined).subscribe( + failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when space name is null', () => { + spaceService.getSpaceByName('somebody', null).subscribe( + failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when space name is an empty string', () => { + spaceService.getSpaceByName('somebody', '').subscribe( + failBecauseErrorIsExpected, verifyErrorNotEmpty); }); }); @@ -198,14 +237,12 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(httpClient, undefined, undefined, WIT_API_URL); spyOn(spaceService as any, 'resolveOwner').and.returnValue(observableOf(cloneDeep(spaces[0]))); - spaceService.getSpaceByName('somebody@fabric8.org', 'my worksp@ce').subscribe(data => { + spaceService.getSpaceByName('somebody@fabric8.org', 'my worksp@ce').subscribe(returnValue => { expect(httpClient.get).toHaveBeenCalledTimes(1); expect(httpClient.get.calls.mostRecent().args[0]).toContain( WIT_API_URL + 'namedspaces/somebody%40fabric8.org/my%20worksp%40ce'); expect(spaceService['resolveOwner']).toHaveBeenCalledTimes(1); - expect(data.id).toBe(spaces[0].id); - expect(data.attributes.name).toBe(spaces[0].attributes.name); - expect(data.attributes.description).toBe(spaces[0].attributes.description); + validateReturnValues([returnValue], [spaces[0]]); }); }); @@ -226,6 +263,16 @@ describe('Service: SpaceService', () => { }); describe('#create', () => { + describe('should throw an error', () => { + it('when space is undefined', () => { + spaceService.create(undefined).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when space is null', () => { + spaceService.create(null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + }); + it('should make a correct POST request', () => { let httpRes: any = jasmine.createSpy('HttpResponse'); httpRes.data = cloneDeep(spaces[0]); @@ -236,13 +283,14 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(httpClient, undefined, undefined, WIT_API_URL); spyOn(spaceService as any, 'resolveOwner').and.returnValue(observableOf(cloneDeep(spaces[0]))); - spaceService.create(spaces[0]).subscribe(() => { + spaceService.create(spaces[0]).subscribe((returnValue) => { expect(httpClient.post).toHaveBeenCalledTimes(1); expect(spaceService['resolveOwner']).toHaveBeenCalledTimes(1); expect(httpClient.post.calls.mostRecent().args[0]).toBe(WIT_API_URL + 'spaces'); expect(httpClient.post.calls.mostRecent().args[1]).toBe(JSON.stringify({ data: spaces[0] })); expect(httpClient.post.calls.mostRecent().args[2].headers).toBeDefined(); + validateReturnValues([returnValue], [spaces[0]]); }); }); @@ -263,6 +311,16 @@ describe('Service: SpaceService', () => { }); describe('#update', () => { + describe('should throw an error', () => { + it('when space is undefined', () => { + spaceService.update(undefined).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when space is null', () => { + spaceService.update(null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + }); + it('should make a correct PATCH request', () => { let httpRes: any = jasmine.createSpy('HttpResponse'); httpRes.data = cloneDeep(spaces[0]); @@ -273,12 +331,13 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(httpClient, undefined, undefined, WIT_API_URL); spyOn(spaceService as any, 'resolveOwner').and.returnValue(observableOf(cloneDeep(spaces[0]))); - spaceService.update(spaces[0]).subscribe(() => { + spaceService.update(spaces[0]).subscribe((returnValue) => { expect(httpClient.patch).toHaveBeenCalledTimes(1); expect(spaceService['resolveOwner']).toHaveBeenCalledTimes(1); expect(httpClient.patch.calls.mostRecent().args[0]).toBe(WIT_API_URL + 'spaces/1'); expect(httpClient.patch.calls.mostRecent().args[1]).toBe(JSON.stringify({ data: spaces[0] })); expect(httpClient.patch.calls.mostRecent().args[2].headers).toBeDefined(); + validateReturnValues([returnValue], [spaces[0]]); }); }); @@ -299,6 +358,16 @@ describe('Service: SpaceService', () => { }); describe('#delete', () => { + describe('should throw an error', () => { + it('when space is undefined', () => { + spaceService.delete(undefined).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when space is null', () => { + spaceService.delete(null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + }); + it('should make a correct DELETE request', () => { let httpRes: any = jasmine.createSpy('HttpResponse'); httpRes.data = cloneDeep(spaces[0]); @@ -350,129 +419,228 @@ describe('Service: SpaceService', () => { }); describe('#search', () => { - it('should delegate to function getSpacesDelegate with correct parameters', () => { - spaceService = new SpaceService(undefined, undefined, undefined, WIT_API_URL); - spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + describe('should throw an error', () => { + it('when search text is null', () => { + spaceService.search(null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); - spaceService.search('asdf', 10, 7).subscribe(() => { - expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); - expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); - const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; - expect(params.get('q')).toBe('asdf'); - expect(params.get('page[offset]')).toBe('70'); - expect(params.get('page[limit]')).toBe('10'); + it('when page size is null', () => { + spaceService.search('asdf', null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when page size is -2', () => { + spaceService.search('asdf', -2).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when page size is 0', () => { + spaceService.search('asdf', 0).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when page number is null', () => { + spaceService.search('asdf', 10, null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when page number is -1', () => { + spaceService.search('asdf', 10, -1).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); }); - it('should delegate to function getSpacesDelegate with correct parameters when search text is empty', () => { - spaceService = new SpaceService(undefined, undefined, undefined, WIT_API_URL); + describe('should delegate to the function getSpacesDelegate', () => { + beforeEach(() => { + spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + }); - spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + it('when called without parameters', () => { + spaceService.search().subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); + const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; + expect(params.get('q')).toBe('*'); + expect(params.get('page[offset]')).toBe('0'); + expect(params.get('page[limit]')).toBe('20'); + validateReturnValues(returnValue, spaces); + }); + }); - spaceService.search('', 10, 7).subscribe(() => { - expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); - expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); - const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; - expect(params.get('q')).toBe('*'); - expect(params.get('page[offset]')).toBe('70'); - expect(params.get('page[limit]')).toBe('10'); + it('when called with correct parameters (asdf)', () => { + spaceService.search('asdf').subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); + const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; + expect(params.get('q')).toBe('asdf'); + expect(params.get('page[offset]')).toBe('0'); + expect(params.get('page[limit]')).toBe('20'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called with correct parameters (*)', () => { + spaceService.search('*').subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); + const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; + expect(params.get('q')).toBe('*'); + expect(params.get('page[offset]')).toBe('0'); + expect(params.get('page[limit]')).toBe('20'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called with correct parameters (\'\')', () => { + spaceService.search('').subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); + const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; + expect(params.get('q')).toBe('*'); + expect(params.get('page[offset]')).toBe('0'); + expect(params.get('page[limit]')).toBe('20'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called with correct parameters (asdf, 1)', () => { + spaceService.search('asdf', 1).subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); + const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; + expect(params.get('q')).toBe('asdf'); + expect(params.get('page[offset]')).toBe('0'); + expect(params.get('page[limit]')).toBe('1'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called with correct parameters (asdf, undefined, 0)', () => { + spaceService.search('asdf', undefined, 0).subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); + const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; + expect(params.get('q')).toBe('asdf'); + expect(params.get('page[offset]')).toBe('0'); + expect(params.get('page[limit]')).toBe('20'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called with correct parameters (asdf, 10, 7)', () => { + spaceService.search('asdf', 10, 7).subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(WIT_API_URL + 'search/spaces'); + const params: HttpParams = (spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[1]; + expect(params.get('q')).toBe('asdf'); + expect(params.get('page[offset]')).toBe('70'); + expect(params.get('page[limit]')).toBe('10'); + validateReturnValues(returnValue, spaces); + }); }); }); }); describe('#getMoreSearchResults', () => { it('should throw an error when nextLink is not defined', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); - - spaceService.getMoreSearchResults().subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); - }); + spaceService.getMoreSearchResults().subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); it('should delegate to function getSpacesDelegate with a correct URL', () => { const nextLink = 'fake-next-link'; - spaceService = new SpaceService(undefined, undefined, undefined, undefined); spaceService['nextLink'] = nextLink; spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); - spaceService.getMoreSearchResults().subscribe(() => { + spaceService.getMoreSearchResults().subscribe((returnValue) => { expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(nextLink); + validateReturnValues(returnValue, spaces); }); }); }); describe('#getSpacesByName', () => { - it('should handle error when limit set to an invalid value', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); + describe('should throw an error', () => { + it('when page size is 0', () => { + spaceService.getSpacesByName('somebody', 0).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); - spaceService.getSpacesByName('somebody', -2).subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); + it('when page number is null', () => { + spaceService.getSpacesByName('somebody', null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); - }); - it('should handle error when user name is empty', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); + it('when page number is -1', () => { + spaceService.getSpacesByName('somebody', -1).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); - spaceService.getSpacesByName('').subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); + it('when user name is undefined', () => { + spaceService.getSpacesByName(undefined).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when user name is null', () => { + spaceService.getSpacesByName(null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when user name is an empty string', () => { + spaceService.getSpacesByName('').subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); }); - it('should delegate to function getSpacesDelegate with correct params', () => { - spaceService = new SpaceService(undefined, undefined, undefined, WIT_API_URL); - spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + describe('should delegate to function getSpacesDelegate', () => { + beforeEach(() => { + spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); + }); - spaceService.getSpacesByName('somebody@fabric8.org', 10).subscribe(() => { - expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); - expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain( - WIT_API_URL + 'namedspaces/somebody%40fabric8.org'); - expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain('page[limit]=10'); + it('when called with correct params (somebody@fabric8.org, 10)', () => { + spaceService.getSpacesByName('somebody@fabric8.org', 10).subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain( + WIT_API_URL + 'namedspaces/somebody%40fabric8.org'); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain('page[limit]=10'); + validateReturnValues(returnValue, spaces); + }); + }); + + it('when called with correct params (somebody@fabric8.org)', () => { + spaceService.getSpacesByName('somebody@fabric8.org').subscribe((returnValue) => { + expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain( + WIT_API_URL + 'namedspaces/somebody%40fabric8.org'); + expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toContain('page[limit]=20'); + validateReturnValues(returnValue, spaces); + }); }); }); }); describe('#getMoreSpacesByName', () => { it('should throw an error when nextLink is not defined', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); - - spaceService.getMoreSpacesByName().subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); - }); + spaceService.getMoreSpacesByName().subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); it('should delegate to function getSpacesDelegate with a correct URL', () => { const nextLink = 'fake-next-link'; - spaceService = new SpaceService(undefined, undefined, undefined, undefined); spaceService['nextLink'] = nextLink; spyOn(spaceService as any, 'getSpacesDelegate').and.returnValue(observableOf(cloneDeep(spaces))); - spaceService.getMoreSpacesByName().subscribe(() => { + spaceService.getMoreSpacesByName().subscribe((returnValue) => { expect(spaceService['getSpacesDelegate']).toHaveBeenCalledTimes(1); expect((spaceService['getSpacesDelegate'] as any).calls.mostRecent().args[0]).toBe(nextLink); + validateReturnValues(returnValue, spaces); }); }); }); describe('#getSpaceById', () => { - it('should throw an error when spaceId is not defined', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); + describe('should throw an error', () => { + it('when space ID is undefined', () => { + spaceService.getSpaceById(undefined).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); - spaceService.getSpaceById(undefined).subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBeDefined(); + it('when space ID is null', () => { + spaceService.getSpaceById(null).subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); + }); + + it('when space ID is an empty string', () => { + spaceService.getSpaceById('').subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); }); @@ -486,13 +654,11 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(httpClient, undefined, undefined, WIT_API_URL); spyOn(spaceService as any, 'resolveOwner').and.returnValue(observableOf(cloneDeep(spaces[0]))); - spaceService.getSpaceById('some@id').subscribe(data => { + spaceService.getSpaceById('some@id').subscribe(returnValue => { expect(httpClient.get).toHaveBeenCalledTimes(1); expect(httpClient.get.calls.mostRecent().args[0]).toContain(WIT_API_URL + 'spaces/some%40id'); expect(spaceService['resolveOwner']).toHaveBeenCalledTimes(1); - expect(data.id).toBe(spaces[0].id); - expect(data.attributes.name).toBe(spaces[0].attributes.name); - expect(data.attributes.description).toBe(spaces[0].attributes.description); + validateReturnValues([returnValue], [spaces[0]]); }); }); @@ -503,7 +669,7 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(httpClient, undefined, undefined, WIT_API_URL); spyOn(spaceService as any, 'handleError').and.returnValue(observableThrowError('handled HTTP error')); - spaceService.getSpaceById('some@id').subscribe(() => { }, err => { + spaceService.getSpaceById('some@id').subscribe(failBecauseErrorIsExpected, err => { expect(httpClient.get).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledWith('some HTTP error'); @@ -514,10 +680,8 @@ describe('Service: SpaceService', () => { describe('#getTotalCount', () => { it('should return the stored value', () => { - spaceService = new SpaceService(undefined, undefined, undefined, undefined); - - spaceService.getTotalCount().subscribe(value => { - expect(value).toBe(spaceService['totalCount']); + spaceService.getTotalCount().subscribe(returnValue => { + expect(returnValue).toBe(spaceService['totalCount']); }); }); }); @@ -540,35 +704,25 @@ describe('Service: SpaceService', () => { spyOn(spaceService as any, 'resolveOwners').and.returnValue(observableOf(cloneDeep(spaces))); }); - it('should make a correct GET request without params', () => { - spaceService['getSpacesDelegate'](fakeUrl).subscribe(data => { + it('should make a correct GET request when called without params', () => { + spaceService['getSpacesDelegate'](fakeUrl).subscribe(returnValue => { expect(httpClient.get).toHaveBeenCalledTimes(1); expect(httpClient.get.calls.mostRecent().args[0]).toBe(fakeUrl); expect(spaceService['resolveOwners']).toHaveBeenCalledTimes(1); - expect(data.length).toBe(2); - for (let i = 0; i < 2; i++) { - expect(data[i].id).toBe(spaces[i].id); - expect(data[i].attributes.name).toBe(spaces[i].attributes.name); - expect(data[i].attributes.description).toBe(spaces[i].attributes.description); - } + validateReturnValues(returnValue, spaces); expect(spaceService['totalCount']).toBe(2); expect(spaceService['nextLink']).toBe('fake-next-link'); }); }); - it('should make a correct GET request with params', () => { + it('should make a correct GET request when called with correct params', () => { const params: HttpParams = new HttpParams().set('param1', 'xxx'); - spaceService['getSpacesDelegate'](fakeUrl, params).subscribe(data => { + spaceService['getSpacesDelegate'](fakeUrl, params).subscribe(returnValue => { expect(httpClient.get).toHaveBeenCalledTimes(1); expect(httpClient.get.calls.mostRecent().args[0]).toBe(fakeUrl); expect(spaceService['resolveOwners']).toHaveBeenCalledTimes(1); - expect(data.length).toBe(2); - for (let i = 0; i < 2; i++) { - expect(data[i].id).toBe(spaces[i].id); - expect(data[i].attributes.name).toBe(spaces[i].attributes.name); - expect(data[i].attributes.description).toBe(spaces[i].attributes.description); - } + validateReturnValues(returnValue, spaces); expect(spaceService['totalCount']).toBe(2); expect(spaceService['nextLink']).toBe('fake-next-link'); }); @@ -581,7 +735,7 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(httpClient, undefined, undefined, WIT_API_URL); spyOn(spaceService as any, 'handleError').and.returnValue(observableThrowError('handled HTTP error')); - spaceService['getSpacesDelegate'](fakeUrl).subscribe(() => { }, err => { + spaceService['getSpacesDelegate'](fakeUrl).subscribe(failBecauseErrorIsExpected, err => { expect(httpClient.get).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledWith('some HTTP error'); @@ -599,19 +753,13 @@ describe('Service: SpaceService', () => { spaceService['handleError']('some error').subscribe(() => { expect(logger.error).toHaveBeenCalledTimes(1); expect(logger.error.calls.mostRecent().args[0]).toBe('some error'); - }, err => { - expect(err).toBeDefined(); - }); + }, verifyErrorNotEmpty); }); it('should wrap the error in an observable', () => { spaceService = new SpaceService(undefined, logger, undefined, undefined); - spaceService['handleError']('some error').subscribe(() => { - fail('An error is expected'); - }, err => { - expect(err).toBe('some error'); - }); + spaceService['handleError']('some error').subscribe(failBecauseErrorIsExpected, verifyErrorNotEmpty); }); }); @@ -622,9 +770,7 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(undefined, undefined, undefined, undefined); - spaceService['resolveOwner'](testData).subscribe(() => { - fail('An error is expected'); - }, err => { + spaceService['resolveOwner'](testData).subscribe(failBecauseErrorIsExpected, err => { expect(err).toBeDefined(); expect(testData.relationalData).toBeDefined(); expect(testData.relationalData.creator).toBeNull(); @@ -637,14 +783,14 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(undefined, undefined, userService, undefined); - spaceService['resolveOwner'](cloneDeep(spaces[0])).subscribe((value: Space) => { + spaceService['resolveOwner'](cloneDeep(spaces[0])).subscribe((returnValue) => { expect(userService.getUserByUserId).toHaveBeenCalledTimes(1); - expect(value.relationalData).toBeDefined(); - expect(value.relationalData.creator.id).toBe(users[0].id); - expect(value.relationalData.creator.type).toBe(users[0].type); - expect(value.relationalData.creator.attributes.fullName).toBe(users[0].attributes.fullName); - expect(value.relationalData.creator.attributes.imageURL).toBe(users[0].attributes.imageURL); - expect(value.relationalData.creator.attributes.username).toBe(users[0].attributes.username); + expect(returnValue.relationalData).toBeDefined(); + expect(returnValue.relationalData.creator.id).toBe(users[0].id); + expect(returnValue.relationalData.creator.type).toBe(users[0].type); + expect(returnValue.relationalData.creator.attributes.fullName).toBe(users[0].attributes.fullName); + expect(returnValue.relationalData.creator.attributes.imageURL).toBe(users[0].attributes.imageURL); + expect(returnValue.relationalData.creator.attributes.username).toBe(users[0].attributes.username); }); }); @@ -655,7 +801,7 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(undefined, undefined, userService, undefined); spyOn(spaceService as any, 'handleError').and.returnValue(observableThrowError('handled HTTP error')); - spaceService['resolveOwner'](cloneDeep(spaces[0])).subscribe(() => { }, err => { + spaceService['resolveOwner'](cloneDeep(spaces[0])).subscribe(failBecauseErrorIsExpected, err => { expect(userService.getUserByUserId).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledWith('some HTTP error'); @@ -679,23 +825,23 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(undefined, undefined, userService, undefined); - spaceService['resolveOwners'](testData).subscribe((value: Space[]) => { + spaceService['resolveOwners'](testData).subscribe((returnValue: Space[]) => { expect(userService.getUserByUserId).toHaveBeenCalledTimes(2); expect(userService.getUserByUserId.calls.argsFor(0)[0]).toEqual(spaces[0].relationships['owned-by'].data.id); expect(userService.getUserByUserId.calls.argsFor(1)[0]).toEqual(spaces[1].relationships['owned-by'].data.id); - expect(value.length).toBe(3); + expect(returnValue.length).toBe(3); for (let i = 0; i < 2; i++) { - expect(value[i].relationalData).toBeDefined(); - expect(value[i].relationalData.creator.type).toBeDefined(); - expect(value[i].relationalData.creator.attributes.fullName).toBeDefined(); - expect(value[i].relationalData.creator.attributes.imageURL).toBeDefined(); - expect(value[i].relationalData.creator.attributes.username).toBeDefined(); + expect(returnValue[i].relationalData).toBeDefined(); + expect(returnValue[i].relationalData.creator.type).toBeDefined(); + expect(returnValue[i].relationalData.creator.attributes.fullName).toBeDefined(); + expect(returnValue[i].relationalData.creator.attributes.imageURL).toBeDefined(); + expect(returnValue[i].relationalData.creator.attributes.username).toBeDefined(); } - expect(value[0].relationalData.creator.id).toBe(spaces[0].relationships['owned-by'].data.id); - expect(value[1].relationalData.creator.id).toBe(spaces[1].relationships['owned-by'].data.id); - expect(value[2].relationalData.creator.id).toBe(spaces[1].relationships['owned-by'].data.id); + expect(returnValue[0].relationalData.creator.id).toBe(spaces[0].relationships['owned-by'].data.id); + expect(returnValue[1].relationalData.creator.id).toBe(spaces[1].relationships['owned-by'].data.id); + expect(returnValue[2].relationalData.creator.id).toBe(spaces[1].relationships['owned-by'].data.id); }); }); @@ -706,7 +852,7 @@ describe('Service: SpaceService', () => { spaceService = new SpaceService(undefined, undefined, userService, undefined); spyOn(spaceService as any, 'handleError').and.returnValue(observableThrowError('handled HTTP error')); - spaceService['resolveOwners'](spaces).subscribe(() => { }, err => { + spaceService['resolveOwners'](spaces).subscribe(failBecauseErrorIsExpected, err => { expect(userService.getUserByUserId).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledTimes(1); expect(spaceService['handleError']).toHaveBeenCalledWith('some HTTP error'); @@ -715,3 +861,20 @@ describe('Service: SpaceService', () => { }); }); }); + +let validateReturnValues = (returnValues: Space[], expectedValues: Space[]) => { + expect(returnValues.length).toBe(expectedValues.length); + for (let i = 0; i < expectedValues.length; i++) { + expect(returnValues[i].id).toBe(expectedValues[i].id); + expect(returnValues[i].attributes.name).toBe(expectedValues[i].attributes.name); + expect(returnValues[i].attributes.description).toBe(expectedValues[i].attributes.description); + } +}; + +let failBecauseErrorIsExpected = () => { + fail('An error is expected'); +}; + +let verifyErrorNotEmpty = (err: any) => { + expect(err).toBeDefined(); +}; diff --git a/src/app/spaces/space.service.ts b/src/app/spaces/space.service.ts index 861e26c..a82b4bf 100644 --- a/src/app/spaces/space.service.ts +++ b/src/app/spaces/space.service.ts @@ -47,8 +47,8 @@ export class SpaceService { } getSpaces(pageSize: number = 20): Observable { - if (pageSize <= 0) { - return observableThrowError('Page limit cannot be less or equal 0'); + if (!pageSize || pageSize <= 0) { + return observableThrowError('Page limit cannot be undefined or less or equal 0'); } const url: string = `${this.spacesUrl}?page[limit]=${pageSize}`; return this.getSpacesDelegate(url); @@ -126,11 +126,11 @@ export class SpaceService { if (!searchText) { return observableThrowError('Search query cannot be undefined'); } - if (pageSize <= 0) { - return observableThrowError('Page limit cannot be less or equal 0'); + if (!pageSize || pageSize <= 0) { + return observableThrowError('Page limit cannot be undefined or less or equal 0'); } - if (pageNumber < 0) { - return observableThrowError('Page offset cannot be less than 0'); + if (typeof pageNumber !== 'number' || pageNumber < 0) { + return observableThrowError('Page offset cannot be undefined or less than 0'); } const url: string = this.searchSpacesUrl; const params: HttpParams = new HttpParams().set('q', searchText) @@ -158,7 +158,7 @@ export class SpaceService { // Currently serves to fetch the list of all spaces owned by a user. getSpacesByName(userName: string, pageSize: number = 20): Observable { if (!userName) { - return observableThrowError('User name cannot be empty'); + return observableThrowError('User name cannot be undefined or empty'); } if (pageSize <= 0) { return observableThrowError('Page limit cannot be less or equal 0');