import { expect } from '../../test'; import * as test from '../test'; import { GraphqlEvent, GraphqlQueryEvent, GraphqlMutationEvent, GraphqlErrorEvent, } from '../types'; const { QUERY_DATA, query, MUTATION_DATA, mutation } = test.data; describe('GraphqlClient', () => { describe('.query', () => { it('runs', async () => { test.mock(QUERY_DATA); const client = test.client(); const result = await client.query({ query }); expect(result.data).to.eql(QUERY_DATA); expect(result.loading).to.eql(false); expect(result.networkStatus).to.eql(7); expect(result.stale).to.eql(false); }); it.skip('fires QUERY observable events', async () => { test.mock(QUERY_DATA); const client = test.client(); const events: GraphqlEvent[] = []; client.events$.subscribe(e => events.push(e)); const options = { query }; const result = await client.query(options); expect(events.length).to.eql(2); expect(events[0].id).to.contain('query:'); expect(events[0].id).to.eql(events[1].id); const payload1 = events[0].payload as GraphqlQueryEvent; const payload2 = events[1].payload as GraphqlQueryEvent; expect(events[0].type).to.eql('QUERY_START'); expect(payload1.options).to.eql(options); expect(payload1.result).to.eql(undefined); expect(events[1].type).to.eql('QUERY_COMPLETE'); expect(payload2.options).to.eql(options); expect(payload2.result).to.eql(result); }); it.skip('fires query error', async () => { test.mockError(400); const client = test.client(); const events: GraphqlEvent[] = []; client.events$.subscribe(e => events.push(e)); try { await client.query({ query }); } catch (err) { expect(events.length).to.eql(2); expect(events[0].id).to.contain('query:'); expect(events[1].id).to.contain('error:'); const errorPayload = events[1].payload as GraphqlErrorEvent; const error = errorPayload.networkError; expect(events[0].type).to.eql('QUERY_START'); expect(events[1].type).to.eql('ERROR'); expect(error && error.message).to.contain( 'Response not successful: Received status code 400', ); } }); }); describe('.query$', () => { it('runs', done => { test.mock(QUERY_DATA); const client = test.client(); const result$ = client.query$({ query }); result$.subscribe(result => { expect(result.data).to.eql(QUERY_DATA); expect(result.loading).to.eql(false); expect(result.networkStatus).to.eql(7); expect(result.stale).to.eql(false); done(); }); }); it.skip('fires QUERY observable events', done => { test.mock(QUERY_DATA); const client = test.client(); const events: GraphqlEvent[] = []; client.events$.subscribe(e => events.push(e)); client // Query. .query$({ query }) .subscribe(result => { expect(events.length).to.eql(2); done(); }); }); }); describe('mutate', () => { it('runs', async () => { test.mock(MUTATION_DATA); const client = test.client(); const result = await client.mutate({ mutation }); expect(result.data).to.eql(MUTATION_DATA); }); it.skip('fires MUTATION observable events', async () => { test.mock(MUTATION_DATA); const client = test.client(); const events: GraphqlEvent[] = []; client.events$.subscribe(e => events.push(e)); const options = { mutation }; const result = await client.mutate(options); expect(events.length).to.eql(2); expect(events[0].id).to.contain('mutate:'); expect(events[0].id).to.eql(events[1].id); const payload1 = events[0].payload as GraphqlMutationEvent; const payload2 = events[1].payload as GraphqlMutationEvent; expect(events[0].type).to.eql('MUTATION_START'); expect(payload1.options).to.eql(options); expect(payload1.result).to.eql(undefined); expect(events[1].type).to.eql('MUTATION_COMPLETE'); expect(payload2.options).to.eql(options); expect(payload2.result).to.eql(result); }); }); });