From 6d6119ad1a01b16afb81fb5390d684782c38d6ce Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Thu, 27 Oct 2016 23:44:02 +0200 Subject: [PATCH 1/2] test(ApolloModule): whole metadata and withClient --- tests/ApolloModule.spec.ts | 51 ++++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 ++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/ApolloModule.spec.ts diff --git a/tests/ApolloModule.spec.ts b/tests/ApolloModule.spec.ts new file mode 100644 index 000000000..9f4e8678c --- /dev/null +++ b/tests/ApolloModule.spec.ts @@ -0,0 +1,51 @@ +import { ApolloClient } from 'apollo-client'; + +import './_common'; + +import { ApolloModule, SelectPipe, Angular2Apollo } from '../src'; +import { angularApolloClient } from '../src/Angular2Apollo'; + +describe('ApolloModule', () => { + let metadata: any = Reflect.getMetadata('annotations', ApolloModule)[0]; + + it('should contain SelectPipe in declarations', () => { + expect(include(metadata.declarations, SelectPipe)).toBe(true); + }); + + it('should export SelectPipe', () => { + expect(include(metadata.exports, SelectPipe)).toBe(true); + }); + + it('should not export Angular2Apollo', () => { + expect(include(metadata.exports, SelectPipe)).toBe(true); + }); + + it('should contain Angular2Apollo in providers', () => { + expect(include(metadata.exports, Angular2Apollo)).toBe(false); + }); + + it('should has withClient method', () => { + expect(ApolloModule.withClient).toBeDefined(); + }); + + describe('withClient', () => { + const client = {} as ApolloClient; + const result = ApolloModule.withClient(client); + + it('should contain ApolloModule as ngModule', () => { + expect(result.ngModule).toBe(ApolloModule); + }); + + it('should contain provider with useValue', () => { + expect(result.providers[0]['useValue']).toBe(client); + }); + + it('should contain provider that provide angularApolloClient', () => { + expect(result.providers[0]['provide']).toBe(angularApolloClient); + }); + }); +}); + +function include(source: any[], find: any): boolean { + return source.some(i => i === find); +} diff --git a/tsconfig.json b/tsconfig.json index 31e26bb3a..752cd441c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "typings.d.ts", "src/index.ts", "tests/Angular2Apollo.spec.ts", - "tests/SelectPipe.spec.ts" + "tests/SelectPipe.spec.ts", + "tests/ApolloModule.spec.ts" ] } From 02a491b64290079a2e7c3b09e8eed4c327d1ac01 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Thu, 27 Oct 2016 23:55:42 +0200 Subject: [PATCH 2/2] test(Angular2Apollo): subscribe method --- tests/Angular2Apollo.spec.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Angular2Apollo.spec.ts b/tests/Angular2Apollo.spec.ts index 2c118aafb..7be1420c2 100644 --- a/tests/Angular2Apollo.spec.ts +++ b/tests/Angular2Apollo.spec.ts @@ -197,6 +197,36 @@ describe('angular2Apollo', () => { expect(result).toEqual('mutate'); }); }); + + describe('subscribe', () => { + it('should throw an error if method is missing', () => { + client.subscribe = undefined; + + expect(() => { + angular2Apollo.subscribe({}); + }).toThrowError('subscriptions'); + }); + + it('should be called with the same options and return Observable', (done) => { + const options = {query: '', variables: {}}; + + spyOn(client, 'subscribe').and.returnValue(['subscription']); + + const obs = angular2Apollo.subscribe(options); + + expect(client.subscribe).toHaveBeenCalledWith(options); + + obs.subscribe({ + next(result) { + expect(result).toBe('subscription'); + done(); + }, + error(error) { + done(new Error('should not be called')); + }, + }); + }); + }); }); describe('defaultApolloClient', () => {