|
1 |
| -import { |
2 |
| - Observable, |
3 |
| -} from 'rxjs/Observable'; |
4 |
| - |
5 | 1 | import {
|
6 | 2 | ApolloQueryObservable,
|
7 | 3 | } from '../src/apolloQueryObservable';
|
8 | 4 |
|
| 5 | +import { |
| 6 | + mockClient, |
| 7 | +} from './_mocks'; |
| 8 | + |
| 9 | +import gql from 'graphql-tag'; |
| 10 | + |
9 | 11 | import 'rxjs/add/operator/map';
|
10 | 12 |
|
11 |
| -class ObservableQuery<T> extends Observable<T> { |
12 |
| - refetch(v?: any) {} |
13 |
| - stopPolling() {} |
14 |
| - startPolling(n?: any) {} |
15 |
| -} |
| 13 | +const query = gql` |
| 14 | + query heroes { |
| 15 | + allHeroes { |
| 16 | + heroes { |
| 17 | + name |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | +`; |
| 22 | +const data = { |
| 23 | + allHeroes: { |
| 24 | + heroes: [{ name: 'Mr Foo' }, { name: 'Mr Bar' }], |
| 25 | + }, |
| 26 | +}; |
16 | 27 |
|
17 | 28 | describe('ApolloQueryObservable', () => {
|
18 |
| - it('should be able to call refetch()', () => { |
19 |
| - const obs = new ObservableQuery(); |
20 |
| - const res = new ApolloQueryObservable(obs); |
21 |
| - const variables = { foo: true }; |
22 |
| - |
23 |
| - spyOn(obs, 'refetch').and.returnValue('refetch'); |
| 29 | + let obsApollo; |
| 30 | + let obsQuery; |
| 31 | + let client; |
24 | 32 |
|
25 |
| - expect(res.refetch(variables)).toEqual('refetch'); |
26 |
| - expect(obs.refetch).toHaveBeenCalledWith(variables); |
| 33 | + beforeEach(() => { |
| 34 | + client = mockClient({ |
| 35 | + request: { query }, |
| 36 | + result: { data }, |
| 37 | + }); |
| 38 | + obsApollo = client.watchQuery({ query }); |
| 39 | + obsQuery = new ApolloQueryObservable(obsApollo); |
27 | 40 | });
|
28 | 41 |
|
29 |
| - it('should be able to call stopPolling()', () => { |
30 |
| - const obs = new ObservableQuery(); |
31 |
| - const res = new ApolloQueryObservable(obs); |
| 42 | + describe('regular', () => { |
| 43 | + it('should be able to subscribe', () => { |
| 44 | + expect(() => { |
| 45 | + obsQuery.subscribe({ |
| 46 | + next() {}, |
| 47 | + }); |
| 48 | + }).not.toThrow(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should be able to receive data', (done) => { |
| 52 | + obsQuery.subscribe({ |
| 53 | + next(result) { |
| 54 | + expect(result.data).toEqual(data); |
| 55 | + done(); |
| 56 | + } |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should be able to receive an error', (done) => { |
| 61 | + obsQuery.subscribe({ |
| 62 | + next() {}, |
| 63 | + }); |
32 | 64 |
|
33 |
| - spyOn(obs, 'stopPolling').and.returnValue('stopPolling'); |
| 65 | + obsQuery.subscribe({ |
| 66 | + error(error) { |
| 67 | + expect(error instanceof Error).toBe(true); |
| 68 | + done(); |
| 69 | + }, |
| 70 | + }) |
| 71 | + }); |
34 | 72 |
|
35 |
| - expect(res.stopPolling()).toEqual('stopPolling'); |
36 |
| - expect(obs.stopPolling).toHaveBeenCalled(); |
| 73 | + it('should be able to use a operator', (done) => { |
| 74 | + obsQuery.map(result => result.data).subscribe({ |
| 75 | + next(result) { |
| 76 | + expect(result).toEqual(data); |
| 77 | + done(); |
| 78 | + }, |
| 79 | + }); |
| 80 | + }); |
37 | 81 | });
|
38 | 82 |
|
39 |
| - it('should be able to call custom method after operator', () => { |
40 |
| - const obs = new ObservableQuery(); |
41 |
| - spyOn(obs, 'refetch').and.returnValue('refetch'); |
| 83 | + describe('apollo-specific', () => { |
| 84 | + it('should be able to refech', () => { |
| 85 | + spyOn(obsApollo, 'refetch').and.returnValue('promise'); |
| 86 | + |
| 87 | + const arg = 'foo'; |
| 88 | + const result = obsQuery.refetch(arg); |
| 89 | + |
| 90 | + expect(obsApollo.refetch).toHaveBeenCalledWith(arg); |
| 91 | + expect(result).toBe('promise'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should be able to startPolling', () => { |
| 95 | + spyOn(obsApollo, 'startPolling'); |
| 96 | + |
| 97 | + const arg = 200; |
| 98 | + obsQuery.startPolling(arg); |
| 99 | + |
| 100 | + expect(obsApollo.startPolling).toHaveBeenCalledWith(arg); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should be able to stopPolling', () => { |
| 104 | + spyOn(obsApollo, 'stopPolling'); |
| 105 | + |
| 106 | + obsQuery.stopPolling(); |
| 107 | + |
| 108 | + expect(obsApollo.stopPolling).toHaveBeenCalled(); |
| 109 | + }); |
42 | 110 |
|
43 |
| - const res = new ApolloQueryObservable(obs).map((i) => i); |
| 111 | + it('should be able to fetchMore', () => { |
| 112 | + spyOn(obsApollo, 'fetchMore'); |
44 | 113 |
|
| 114 | + const arg = 200; |
| 115 | + obsQuery.fetchMore(arg); |
45 | 116 |
|
46 |
| - expect(res['refetch']()).toEqual('refetch'); |
47 |
| - expect(obs.refetch).toHaveBeenCalled(); |
| 117 | + expect(obsApollo.fetchMore).toHaveBeenCalledWith(arg); |
| 118 | + }); |
48 | 119 | });
|
49 | 120 | });
|
0 commit comments