From 9ffd4f7d1e11068c3cd60930e0fb0f7dc9d092b1 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Sun, 9 Jan 2022 20:12:40 +1300 Subject: [PATCH] test(type-utils): add test for IndexSignature internals --- .../type-utils/tests/isTypeReadonly.test.ts | 73 +++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/packages/type-utils/tests/isTypeReadonly.test.ts b/packages/type-utils/tests/isTypeReadonly.test.ts index 7153b44d7769..94e6add83372 100644 --- a/packages/type-utils/tests/isTypeReadonly.test.ts +++ b/packages/type-utils/tests/isTypeReadonly.test.ts @@ -176,13 +176,48 @@ describe('isTypeReadonly', () => { describe('Intersection', () => { describe('is readonly', () => { - it('handles an intersection of 2', () => { - const { type, checker } = getType( - `type Test = Readonly<{ foo: string; bar: number; }> & Readonly<{ bar: number; }>;`, - ); + describe('default options', () => { + it('handles an intersection of 2 records', () => { + const { type, checker } = getType( + `type Test = Readonly<{ foo: string; bar: number; }> & Readonly<{ bar: number; }>;`, + ); - const result = isTypeReadonly(checker, type); - expect(result).toBe(true); + const result = isTypeReadonly(checker, type); + expect(result).toBe(true); + }); + + it('handles a readonly intersection of readonly array and readonly record', () => { + const { type, checker } = getType( + `type Test = Readonly & { readonly foo: string; }>;`, + ); + + const result = isTypeReadonly(checker, type); + expect(result).toBe(true); + }); + + it('handles a intersection of readonly readonly array and readonly record', () => { + const { type, checker } = getType( + `type Test = Readonly> & Readonly<{ readonly foo: string; }>;`, + ); + + const result = isTypeReadonly(checker, type); + expect(result).toBe(true); + }); + }); + + describe('treatMethodsAsReadonly', () => { + const options: ReadonlynessOptions = { + treatMethodsAsReadonly: true, + }; + + it('handles an intersection of readonly array and readonly record', () => { + const { type, checker } = getType( + `type Test = ReadonlyArray & { readonly foo: string; };`, + ); + + const result = isTypeReadonly(checker, type, options); + expect(result).toBe(true); + }); }); }); @@ -215,5 +250,31 @@ describe('isTypeReadonly', () => { }); }); }); + + describe('IndexSignature', () => { + describe('is readonly', () => { + it('handles readonly PropertySignature inside a readonly IndexSignature', () => { + const { type, checker } = getType( + `type Test = { readonly [key: string]: { readonly foo: readonly string[]; }; };`, + ); + + const result = isTypeReadonly(checker, type); + expect(result).toBe(true); + }); + }); + + describe('is not readonly', () => { + describe('default options', () => { + it('fails with a mutable PropertySignature inside a readonly IndexSignature', () => { + const { type, checker } = getType( + `type Test = { readonly [key: string]: { foo: string[]; }; };`, + ); + + const result = isTypeReadonly(checker, type); + expect(result).toBe(false); + }); + }); + }); + }); }); });