UNPKG

1.98 kBPlain TextView Raw
1import {
2 Provider, ReflectiveInjector,
3} from '@angular/core';
4
5import ApolloClient from 'apollo-client';
6
7import {
8 APOLLO_PROVIDERS,
9} from '../src/index';
10
11import {
12 Angular2Apollo,
13 defaultApolloClient,
14 angularApolloClient,
15} from '../src/angular2Apollo';
16
17describe('angular2Apollo', () => {
18 const client = new ApolloClient();
19
20 describe('Angular2Apollo', () => {
21 /**
22 * Gets Angular2Apollo service and calls a method
23 *
24 * Checks if method with the same name has been called
25 * with the same same options
26 *
27 * It also checks if service method returns result of ApolloClient method
28 *
29 * @param {string} method Name of method you want to test
30 * @param {any} options Used options
31 * @param {any} result Mock result
32 */
33 function rawApiCall(method: string, options = 'options', result = 'result') {
34 spyOn(client, method).and.returnValue(result);
35
36 const injector = ReflectiveInjector.resolveAndCreate([defaultApolloClient(client), APOLLO_PROVIDERS]);
37 const service = injector.get(Angular2Apollo);
38
39 expect(service[method](options)).toBe(result);
40 expect(client[method]).toHaveBeenCalledWith(options);
41 }
42
43 describe('watchQuery()', () => {
44 it('should call same method on client with same args and return it', () => {
45 rawApiCall('watchQuery');
46 });
47 });
48
49 describe('mutate()', () => {
50 it('should call same method on client with same args and return it', () => {
51 rawApiCall('mutate');
52 });
53 });
54 });
55
56 describe('defaultApolloClient', () => {
57 it('should create a provider', () => {
58 const provider = defaultApolloClient(client);
59 expect(provider instanceof Provider).toBe(true);
60 });
61
62 it('should set a AngularApolloClient', () => {
63 const injector = ReflectiveInjector.resolveAndCreate([defaultApolloClient(client)]);
64 expect(injector.get(angularApolloClient)).toBe(client);
65 });
66 });
67});