Skip to content

Commit

Permalink
test(type-utils): add conditional type tests to isTypeReadonly
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jan 9, 2022
1 parent a137c76 commit ae4a614
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions packages/type-utils/tests/isTypeReadonly.test.ts
Expand Up @@ -215,5 +215,56 @@ describe('isTypeReadonly', () => {
});
});
});

describe('Conditional Types', () => {
describe('is readonly', () => {
it('handles a conditional type that is readonly', () => {
const { type, checker } = getType(
`type Test<T> = T extends readonly number[] ? readonly string[] : readonly number[];`,
);

const result = isTypeReadonly(checker, type);
expect(result).toBe(true);
});

it('should ignore a mutable condition', () => {
const { type, checker } = getType(
`type Test<T> = T extends number[] ? readonly string[] : readonly number[];`,
);

const result = isTypeReadonly(checker, type);
expect(result).toBe(true);
});
});

describe('is not readonly', () => {
it('fails with a conditional type that is mutables', () => {
const { type, checker } = getType(
`type Test<T> = T extends number[] ? string[] : number[];`,
);

const result = isTypeReadonly(checker, type);
expect(result).toBe(false);
});

it('fails with a conditional type that is mutable or readonly', () => {
const { type, checker } = getType(
`type Test<T> = T extends number[] ? string[] : readonly number[];`,
);

const result = isTypeReadonly(checker, type);
expect(result).toBe(false);
});

it('fails with a conditional type that is readonly or mutable', () => {
const { type, checker } = getType(
`type Test<T> = T extends number[] ? readonly string[] : number[];`,
);

const result = isTypeReadonly(checker, type);
expect(result).toBe(false);
});
});
});
});
});

0 comments on commit ae4a614

Please sign in to comment.