Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): don't convert null to a string when flushing a mock request #21417

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/common/http/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ load("//tools:defaults.bzl", "ts_library")
ts_library(
name = "testing",
testonly = 1,
srcs = glob(["**/*.ts"]),
srcs = glob(
[
"*.ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this change needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This directory previously did not have tests, so this package globbed all the files together. Now that it has tests, we need to somehow exclude them from the build.

"src/**/*.ts",
],
),
module_name = "@angular/common/http/testing",
deps = [
"//packages/common/http",
Expand Down
15 changes: 3 additions & 12 deletions packages/common/http/testing/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,17 @@ function _maybeConvertBody(
responseType: string, body: ArrayBuffer | Blob | string | number | Object |
(string | number | Object | null)[] | null): ArrayBuffer|Blob|string|number|Object|
(string | number | Object | null)[]|null {
if (body === null) {
return null;
}
switch (responseType) {
case 'arraybuffer':
if (body === null) {
return null;
}
return _toArrayBufferBody(body);
case 'blob':
if (body === null) {
return null;
}
return _toBlob(body);
case 'json':
if (body === null) {
return 'null';
}
return _toJsonBody(body);
case 'text':
if (body === null) {
return null;
}
return _toTextBody(body);
default:
throw new Error(`Unsupported responseType: ${responseType}`);
Expand Down
27 changes: 27 additions & 0 deletions packages/common/http/testing/test/request_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to import custom describe Why is the default one not sufficient?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not necessary for this file, but the common/http tests all use the testing_internal functions, which do things like handle Promise returns, etc.

I could migrate them all off, but I'm not sure if any code changes would be necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would like to get rid of these long term, so let's not add more. Could you remove them from this test.


import {HttpClient} from '../../src/client';
import {HttpClientTestingBackend} from '../src/backend';

describe('HttpClient TestRequest', () => {
it('accepts a null body', () => {
const mock = new HttpClientTestingBackend();
const client = new HttpClient(mock);

let resp: any;
client.post('/some-url', {test: 'test'}).subscribe(body => { resp = body; });

const req = mock.expectOne('/some-url');
req.flush(null);

expect(resp).toBeNull();
});
});