Skip to content

Commit

Permalink
Cover batched XHR cancelling with tests
Browse files Browse the repository at this point in the history
This is dependent on apollographql/apollo-client#9248,
so it is not strictly specific to `apollo-angular`, but it seemed important to
ensure that this package does indeed allow to cancel XHR even when batching is
enabled.
  • Loading branch information
PowerKiKi authored and kamilkisiela committed Feb 16, 2022
1 parent a3beff7 commit 5d8f5bc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@angular/platform-browser": "^13.0.0",
"@angular/platform-browser-dynamic": "^13.0.0",
"@angular/platform-server": "^13.0.0",
"@apollo/client": "3.3.19",
"@apollo/client": "3.5.6",
"@babel/core": "^7.16.0",
"@babel/preset-env": "^7.16.0",
"@schematics/angular": "^13.0.0",
Expand Down
36 changes: 34 additions & 2 deletions packages/apollo-angular/http/tests/http-batch-link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
HttpClientTestingModule,
HttpTestingController,
} from '@angular/common/http/testing';
import {execute, ApolloLink, Operation, gql} from '@apollo/client/core';
import {ApolloLink, execute, gql, Operation} from '@apollo/client/core';

import {HttpBatchLink} from '../src/http-batch-link';

Expand Down Expand Up @@ -57,7 +57,7 @@ describe('HttpBatchLink', () => {
});

setTimeout(() => {
httpBackend.expectOne('graphql').flush({data});
expect(httpBackend.expectOne('graphql').cancelled).toBe(true);
}, 50);
});

Expand Down Expand Up @@ -731,4 +731,36 @@ describe('HttpBatchLink', () => {
});
}, 50);
});

test('should cancel XHR when unsubscribing', (done: jest.DoneCallback) => {
const link = httpLink.create({uri: 'graphql', batchMax: 1});
const op = {
query: gql`
query heroes {
heroes {
name
}
}
`,
operationName: 'heroes',
variables: {},
};

execute(link, op)
.subscribe({
next: () => {
done.fail('Should not be here');
},
error: () => {
done.fail('Should not be here');
},
})
.unsubscribe();

setTimeout(() => {
console.log('TESTING');
expect(httpBackend.expectOne('graphql').cancelled).toBe(true);
done();
}, 50);
});
});
28 changes: 28 additions & 0 deletions packages/apollo-angular/http/tests/http-link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,32 @@ describe('HttpLink', () => {
return true;
});
});

test('should cancel XHR when unsubscribing', () => {
const link = httpLink.create({uri: 'graphql'});
const op = {
query: gql`
query heroes {
heroes {
name
}
}
`,
operationName: 'heroes',
variables: {},
};

execute(link, op)
.subscribe({
next: () => {
throw new Error('Should not be here');
},
error: () => {
throw new Error('Should not be here');
},
})
.unsubscribe();

expect(httpBackend.expectOne('graphql').cancelled).toBe(true);
});
});
79 changes: 40 additions & 39 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,23 @@
tslib "^2.3.0"
xhr2 "^0.2.0"

"@apollo/client@3.3.19":
version "3.3.19"
resolved "https://registry.npmjs.org/@apollo/client/-/client-3.3.19.tgz"
integrity sha512-vzljWLPP0GwocfBhUopzDCUwsiaNTtii1eu8qDybAXqwj4/ZhnIM46c6dNQmnVcJpAIFRIsNCOxM4OlMDySJug==
"@apollo/client@3.5.6":
version "3.5.6"
resolved "https://registry.yarnpkg.com/@apollo/client/-/client-3.5.6.tgz#911929df073280689efd98e5603047b79e0c39a2"
integrity sha512-XHoouuEJ4L37mtfftcHHO1caCRrKKAofAwqRoq28UQIPMJk+e7n3X9OtRRNXKk/9tmhNkwelSary+EilfPwI7A==
dependencies:
"@graphql-typed-document-node/core" "^3.0.0"
"@types/zen-observable" "^0.8.0"
"@wry/context" "^0.6.0"
"@wry/equality" "^0.4.0"
fast-json-stable-stringify "^2.0.0"
graphql-tag "^2.12.0"
"@wry/equality" "^0.5.0"
"@wry/trie" "^0.3.0"
graphql-tag "^2.12.3"
hoist-non-react-statics "^3.3.2"
optimism "^0.16.0"
optimism "^0.16.1"
prop-types "^15.7.2"
symbol-observable "^2.0.0"
ts-invariant "^0.7.0"
tslib "^1.10.0"
zen-observable "^0.8.14"
symbol-observable "^4.0.0"
ts-invariant "^0.9.4"
tslib "^2.3.0"
zen-observable-ts "^1.2.0"

"@assemblyscript/loader@^0.10.1":
version "0.10.1"
Expand Down Expand Up @@ -1974,11 +1973,6 @@
dependencies:
"@types/yargs-parser" "*"

"@types/zen-observable@^0.8.0":
version "0.8.2"
resolved "https://registry.npmjs.org/@types/zen-observable/-/zen-observable-0.8.2.tgz"
integrity sha512-HrCIVMLjE1MOozVoD86622S7aunluLb2PJdPfb3nYiEtohm8mIB/vyv0Fd37AdeMFrTUQXEunw78YloMA3Qilg==

"@webassemblyjs/ast@1.11.1":
version "1.11.1"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.11.1.tgz#2bfd767eae1a6996f432ff7e8d7fc75679c0b6a7"
Expand Down Expand Up @@ -2107,12 +2101,12 @@
dependencies:
tslib "^2.1.0"

"@wry/equality@^0.4.0":
version "0.4.0"
resolved "https://registry.npmjs.org/@wry/equality/-/equality-0.4.0.tgz"
integrity sha512-DxN/uawWfhRbgYE55zVCPOoe+jvsQ4m7PT1Wlxjyb/LCCLuU1UsucV2BbCxFAX8bjcSueFBbB5Qfj1Zfe8e7Fw==
"@wry/equality@^0.5.0":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@wry/equality/-/equality-0.5.2.tgz#72c8a7a7d884dff30b612f4f8464eba26c080e73"
integrity sha512-oVMxbUXL48EV/C0/M7gLVsoK6qRHPS85x8zECofEZOVvxGmIPLA9o5Z27cc2PoAyZz1S2VoM2A7FLAnpfGlneA==
dependencies:
tslib "^2.1.0"
tslib "^2.3.0"

"@wry/trie@^0.3.0":
version "0.3.0"
Expand Down Expand Up @@ -4513,10 +4507,10 @@ graceful-fs@^4.2.6:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.8.tgz#e412b8d33f5e006593cbd3cee6df9f2cebbe802a"
integrity sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==

graphql-tag@^2.12.0:
version "2.12.4"
resolved "https://registry.npmjs.org/graphql-tag/-/graphql-tag-2.12.4.tgz"
integrity sha512-VV1U4O+9x99EkNpNmCUV5RZwq6MnK4+pGbRYWG+lA/m3uo7TSqJF81OkcOP148gFP6fzdl7JWYBrwWVTS9jXww==
graphql-tag@^2.12.3:
version "2.12.6"
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.12.6.tgz#d441a569c1d2537ef10ca3d1633b48725329b5f1"
integrity sha512-FdSNcu2QQcWnM2VNvSCCDCVS5PpPqpzgFT8+GXzqJuoDd0CBncxCY278u4mhRO7tMgo2JjgJA5aZ+nWSQ/Z+xg==
dependencies:
tslib "^2.1.0"

Expand Down Expand Up @@ -6581,9 +6575,9 @@ opencollective-postinstall@^2.0.2:
resolved "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz"
integrity sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==

optimism@^0.16.0:
optimism@^0.16.1:
version "0.16.1"
resolved "https://registry.npmjs.org/optimism/-/optimism-0.16.1.tgz"
resolved "https://registry.yarnpkg.com/optimism/-/optimism-0.16.1.tgz#7c8efc1f3179f18307b887e18c15c5b7133f6e7d"
integrity sha512-64i+Uw3otrndfq5kaoGNoY7pvOhSsjFEN4bdEFh80MWVk/dbgJfMv7VFDeCT8LxNAlEVhQmdVEbfE7X2nWNIIg==
dependencies:
"@wry/context" "^0.6.0"
Expand Down Expand Up @@ -8262,10 +8256,10 @@ supports-hyperlinks@^2.0.0:
has-flag "^4.0.0"
supports-color "^7.0.0"

symbol-observable@^2.0.0:
version "2.0.3"
resolved "https://registry.npmjs.org/symbol-observable/-/symbol-observable-2.0.3.tgz"
integrity sha512-sQV7phh2WCYAn81oAkakC5qjq2Ml0g8ozqz03wOGnx9dDlG1de6yrF+0RAzSJD8fPUow3PTSMf2SAbOGxb93BA==
symbol-observable@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-4.0.0.tgz#5b425f192279e87f2f9b937ac8540d1984b39205"
integrity sha512-b19dMThMV4HVFynSAM1++gBHAbk2Tc/osgLIBZMKsyqh34jb2e8Os7T6ZW/Bt3pJFdBTd2JwAnAAEQV7rSNvcQ==

symbol-tree@^3.2.4:
version "3.2.4"
Expand Down Expand Up @@ -8421,10 +8415,10 @@ tree-kill@1.2.2:
resolved "https://registry.yarnpkg.com/tree-kill/-/tree-kill-1.2.2.tgz#4ca09a9092c88b73a7cdc5e8a01b507b0790a0cc"
integrity sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==

ts-invariant@^0.7.0:
version "0.7.3"
resolved "https://registry.npmjs.org/ts-invariant/-/ts-invariant-0.7.3.tgz"
integrity sha512-UWDDeovyUTIMWj+45g5nhnl+8oo+GhxL5leTaHn5c8FkQWfh8v66gccLd2/YzVmV5hoQUjCEjhrXnQqVDJdvKA==
ts-invariant@^0.9.4:
version "0.9.4"
resolved "https://registry.yarnpkg.com/ts-invariant/-/ts-invariant-0.9.4.tgz#42ac6c791aade267dd9dc65276549df5c5d71cac"
integrity sha512-63jtX/ZSwnUNi/WhXjnK8kz4cHHpYS60AnmA6ixz17l7E12a5puCWFlNpkne5Rl0J8TBPVHpGjsj4fxs8ObVLQ==
dependencies:
tslib "^2.1.0"

Expand Down Expand Up @@ -8946,9 +8940,16 @@ yocto-queue@^0.1.0:
resolved "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zen-observable@^0.8.14:
zen-observable-ts@^1.2.0:
version "1.2.3"
resolved "https://registry.yarnpkg.com/zen-observable-ts/-/zen-observable-ts-1.2.3.tgz#c2f5ccebe812faf0cfcde547e6004f65b1a6d769"
integrity sha512-hc/TGiPkAWpByykMwDcem3SdUgA4We+0Qb36bItSuJC9xD0XVBZoFHYoadAomDSNf64CG8Ydj0Qb8Od8BUWz5g==
dependencies:
zen-observable "0.8.15"

zen-observable@0.8.15:
version "0.8.15"
resolved "https://registry.npmjs.org/zen-observable/-/zen-observable-0.8.15.tgz"
resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.8.15.tgz#96415c512d8e3ffd920afd3889604e30b9eaac15"
integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ==

zone.js@~0.11.4:
Expand Down

0 comments on commit 5d8f5bc

Please sign in to comment.