Skip to content

Commit cd0743a

Browse files
Use spread operator
1 parent 016a673 commit cd0743a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class RPCClient {
1313
call(name, args, headers = {}) {
1414
return fetch(`${this.url}/${name}`, {
1515
method: 'POST',
16-
headers: Object.assign({
16+
headers: {
1717
Accept: 'application/json',
1818
'Content-Type': 'application/json',
19-
}, headers),
19+
...headers,
20+
},
2021
body: JSON.stringify({
2122
args: JSON.stringify(args),
2223
}),

index.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,27 @@ describe('RPCClient', () => {
123123
}
124124
});
125125
});
126+
127+
describe('headers', () => {
128+
it('should call RPC method with headers', async () => {
129+
const name = 'someMethod';
130+
const args = { a: 'a', b: 'b' };
131+
const headers = { foo: 'bar' };
132+
const rpc = new RPCClient();
133+
const response = await rpc.call(name, args, headers);
134+
135+
expect(fetch).toBeCalledWith('http://localhost/someMethod', {
136+
method: 'POST',
137+
headers: {
138+
Accept: 'application/json',
139+
'Content-Type': 'application/json',
140+
...headers,
141+
},
142+
body: JSON.stringify({
143+
args: JSON.stringify(args),
144+
}),
145+
});
146+
expect(response).toEqual(fetch.fakeResponse);
147+
});
148+
});
126149
});

0 commit comments

Comments
 (0)