blob: c075a2d0c5164b2177ee08e3c6d57af2239b44ae [file] [log] [blame]
Adrià Vilanova Martínez9c418ab2024-12-05 15:34:40 +01001/**
2 * @jest-environment ./src/xhrInterceptor/fetchProxy/__environments__/fetchEnvironment.ts
3 */
4
5import { describe, expect, it } from '@jest/globals';
6import FetchHeaders from './FetchHeaders';
7
8describe('FetchHeaders', () => {
9 const dummyHeaderName = 'X-Foo';
10 const dummyHeaderValue = 'bar';
11 const dummyHeadersPerFormat: { description: string; value: HeadersInit }[] = [
12 {
13 description: 'a Headers instance',
14 value: new Headers({ [dummyHeaderName]: dummyHeaderValue }),
15 },
16 {
17 description: 'an object',
18 value: { [dummyHeaderName]: dummyHeaderValue },
19 },
20 {
21 description: 'an array',
22 value: [[dummyHeaderName, dummyHeaderValue]],
23 },
24 ];
25
26 describe.each(dummyHeadersPerFormat)(
27 'when the headers are presented as $description',
28 ({ value }) => {
29 describe('hasValue', () => {
30 it('should return true when the header with the provided value is present', () => {
31 const sut = new FetchHeaders(value);
32 const result = sut.hasValue(dummyHeaderName, dummyHeaderValue);
33
34 expect(result).toBe(true);
35 });
36
37 it("should return true when a header with the provided value is present even if the provided name doesn't have the same case", () => {
38 const sut = new FetchHeaders(value);
39 const result = sut.hasValue('x-FoO', dummyHeaderValue);
40
41 expect(result).toBe(true);
42 });
43
44 it('should return false when a header with the provided value is not present', () => {
45 const sut = new FetchHeaders(value);
46 const result = sut.hasValue('X-NonExistent', dummyHeaderValue);
47
48 expect(result).toBe(false);
49 });
50 });
51
52 describe('removeHeader', () => {
53 it('should remove the header if it is present', () => {
54 const sut = new FetchHeaders(value);
55 sut.removeHeader(dummyHeaderName);
56
57 if (value instanceof Headers) {
58 expect(value.has(dummyHeaderName)).toBe(false);
59 } else if (Array.isArray(value)) {
60 expect(value).not.toContain(
61 expect.arrayContaining([dummyHeaderName]),
62 );
63 } else {
64 expect(value).not.toHaveProperty(dummyHeaderName);
65 }
66 });
67 });
68 },
69 );
70
71 describe('when the headers are presented as undefined', () => {
72 describe('hasValue', () => {
73 it('should return false', () => {
74 const sut = new FetchHeaders(undefined);
75 const result = sut.hasValue(dummyHeaderName, dummyHeaderValue);
76
77 expect(result).toBe(false);
78 });
79 });
80
81 describe('removeHeader', () => {
82 it('should not throw', () => {
83 const sut = new FetchHeaders(undefined);
84 expect(() => sut.removeHeader(dummyHeaderName)).not.toThrow();
85 });
86 });
87 });
88});