diff --git a/src/react/hoc/__tests__/queries/loading.test.tsx b/src/react/hoc/__tests__/queries/loading.test.tsx index 9c0bab58731..b5151a9c4e5 100644 --- a/src/react/hoc/__tests__/queries/loading.test.tsx +++ b/src/react/hoc/__tests__/queries/loading.test.tsx @@ -3,7 +3,7 @@ import { render, wait } from '@testing-library/react'; import gql from 'graphql-tag'; import { DocumentNode } from 'graphql'; -import { ApolloClient } from '../../../../core'; +import { ApolloClient, NetworkStatus } from '../../../../core'; import { ApolloProvider } from '../../../context'; import { InMemoryCache as Cache } from '../../../../cache'; import { itAsync, mockSingleLink } from '../../../../testing'; @@ -181,18 +181,62 @@ describe('[queries] loading', () => { })( class extends React.Component> { componentDidUpdate(prevProps: ChildProps) { - const { data } = this.props; - // variables changed, new query is loading, but old data is still there - if (count === 1) { - if (data!.loading) { - expect(data!.networkStatus).toBe(2); - expect(data!.allPeople).toBeUndefined(); - } else { - expect(prevProps.data!.loading).toBe(true); - expect(data!.networkStatus).toBe(7); - expect(data!.allPeople).toEqual(data2.allPeople); - done = true; + try { + // variables changed, new query is loading, but old data is still there + switch (count) { + case 0: + expect(prevProps.data!.loading).toBe(true); + expect(prevProps.data!.variables).toEqual(variables1); + expect(prevProps.data!.allPeople).toBe(undefined); + expect(prevProps.data!.error).toBe(undefined); + expect(prevProps.data!.networkStatus).toBe(NetworkStatus.loading); + expect(this.props.data!.loading).toBe(false); + expect(this.props.data!.variables).toEqual(variables1); + expect(this.props.data!.allPeople).toEqual(data1.allPeople); + expect(this.props.data!.error).toBe(undefined); + expect(this.props.data!.networkStatus).toBe(NetworkStatus.ready); + break; + case 1: + // TODO: What is this extra render + expect(prevProps.data!.loading).toBe(false); + expect(prevProps.data!.variables).toEqual(variables1); + expect(prevProps.data!.allPeople).toEqual(data1.allPeople); + expect(prevProps.data!.error).toBe(undefined); + expect(prevProps.data!.networkStatus).toBe(NetworkStatus.ready); + expect(this.props.data!.loading).toBe(false); + expect(this.props.data!.variables).toEqual(variables1); + expect(this.props.data!.allPeople).toEqual(data1.allPeople); + expect(this.props.data!.error).toBe(undefined); + expect(prevProps.data!.networkStatus).toBe(NetworkStatus.ready); + break; + case 2: + expect(prevProps.data!.loading).toBe(false); + expect(prevProps.data!.variables).toEqual(variables1); + expect(prevProps.data!.allPeople).toEqual(data1.allPeople); + expect(prevProps.data!.error).toBe(undefined); + expect(this.props.data!.loading).toBe(true); + expect(this.props.data!.variables).toEqual(variables2); + expect(this.props.data!.allPeople).toBe(undefined); + expect(this.props.data!.error).toBe(undefined); + expect(this.props.data!.networkStatus).toBe(NetworkStatus.setVariables); + break; + case 3: + expect(prevProps.data!.loading).toBe(true); + expect(prevProps.data!.variables).toEqual(variables2); + expect(prevProps.data!.allPeople).toBe(undefined); + expect(prevProps.data!.error).toBe(undefined); + expect(prevProps.data!.networkStatus).toBe(NetworkStatus.setVariables); + expect(this.props.data!.loading).toBe(false); + expect(this.props.data!.variables).toEqual(variables2); + expect(this.props.data!.allPeople).toEqual(data2.allPeople); + expect(this.props.data!.error).toBe(undefined); + expect(this.props.data!.networkStatus).toBe(NetworkStatus.ready); + done = true; + break; } + count++; + } catch (err) { + reject(err); } } render() { @@ -206,7 +250,6 @@ describe('[queries] loading', () => { componentDidMount() { setTimeout(() => { - count++; this.setState({ first: 2 }); }, 50); } @@ -222,7 +265,7 @@ describe('[queries] loading', () => { ); - return wait(() => expect(done).toBeTruthy()).then(resolve, reject); + wait(() => expect(done).toBe(true)).then(resolve, reject); }); itAsync('resets the loading state after a refetched query', (resolve, reject) => { @@ -592,25 +635,32 @@ describe('[queries] loading', () => { })( class extends React.Component> { render() { - if (count === 0) { - expect(this.props.data!.loading).toBeTruthy(); // has initial data - } - - if (count === 1) { - expect(this.props.data!.loading).toBeFalsy(); - setTimeout(() => { - this.props.setFirst(2); - }); - } - - if (count === 2) { - expect(this.props.data!.loading).toBeTruthy(); // on variables change + try { + switch (count) { + case 0: + expect(this.props.data!.loading).toBeTruthy(); // has initial data + break; + case 1: + expect(this.props.data!.loading).toBeFalsy(); + setTimeout(() => { + this.props.setFirst(2); + }); + break; + case 2: + expect(this.props.data!.loading).toBeFalsy(); // on variables change + break; + case 3: + expect(this.props.data!.loading).toBeTruthy(); // on variables change + break; + case 4: + // new data after fetch + expect(this.props.data!.loading).toBeFalsy(); + break; + } + } catch (err) { + reject(err); } - if (count === 3) { - // new data after fetch - expect(this.props.data!.loading).toBeFalsy(); - } count++; return null; @@ -625,7 +675,7 @@ describe('[queries] loading', () => { ); - return wait(() => expect(count).toBe(4)).then(resolve, reject); + return wait(() => expect(count).toBe(5)).then(resolve, reject); }); itAsync( @@ -706,27 +756,34 @@ describe('[queries] loading', () => { class extends React.Component> { render() { const { props } = this; - if (count === 0) { - expect(props.data!.loading).toBeTruthy(); + try { + switch (count) { + case 0: + expect(props.data!.loading).toBeTruthy(); + break; + case 1: + setTimeout(() => { + this.props.setFirst(2); + }); + //fallthrough + case 2: + expect(props.data!.loading).toBeFalsy(); // has initial data + expect(props.data!.allPeople).toEqual(data.allPeople); + break; + + case 3: + expect(props.data!.loading).toBeTruthy(); // on variables change + break; + case 4: + // new data after fetch + expect(props.data!.loading).toBeFalsy(); + expect(props.data!.allPeople).toEqual(data.allPeople); + break; + } + } catch (err) { + reject(err); } - if (count === 1) { - expect(props.data!.loading).toBeFalsy(); // has initial data - expect(props.data!.allPeople).toEqual(data.allPeople); - setTimeout(() => { - this.props.setFirst(2); - }); - } - - if (count === 2) { - expect(props.data!.loading).toBeTruthy(); // on variables change - } - - if (count === 3) { - // new data after fetch - expect(props.data!.loading).toBeFalsy(); - expect(props.data!.allPeople).toEqual(data.allPeople); - } count++; return null; } @@ -740,7 +797,7 @@ describe('[queries] loading', () => { ); - return wait(() => expect(count).toBe(4)).then(resolve, reject); + return wait(() => expect(count).toBe(5)).then(resolve, reject); } ); });