Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(type-utils): add basic tests for isTypeReadonly
- Loading branch information
1 parent
974858a
commit ca7410e
Showing
1 changed file
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import * as ts from 'typescript'; | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { parseForESLint } from '@typescript-eslint/parser'; | ||
import { | ||
isTypeReadonly, | ||
type ReadonlynessOptions, | ||
} from '../src/isTypeReadonly'; | ||
import path from 'path'; | ||
|
||
describe('isTypeReadonly', () => { | ||
const rootDir = path.join(__dirname, 'fixtures'); | ||
|
||
describe('TSTypeAliasDeclaration ', () => { | ||
function getType(code: string): { | ||
type: ts.Type; | ||
checker: ts.TypeChecker; | ||
} { | ||
const { ast, services } = parseForESLint(code, { | ||
project: './tsconfig.json', | ||
filePath: path.join(rootDir, 'file.ts'), | ||
tsconfigRootDir: rootDir, | ||
}); | ||
const checker = services.program.getTypeChecker(); | ||
const esTreeNodeToTSNodeMap = services.esTreeNodeToTSNodeMap; | ||
|
||
const declaration = ast.body[0] as TSESTree.TSTypeAliasDeclaration; | ||
return { | ||
type: checker.getTypeAtLocation( | ||
esTreeNodeToTSNodeMap.get(declaration.id), | ||
), | ||
checker, | ||
}; | ||
} | ||
|
||
describe('basics', () => { | ||
describe('is readonly', () => { | ||
describe('default options', () => { | ||
it('handles a record with only readonly props', () => { | ||
const { type, checker } = getType( | ||
`type Test = { readonly bar: string; };`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, type); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('handles a shallowly mutable record wrapped in Readonly', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = Readonly<{ bar: string; }>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('handles an readonly readonly array', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = Readonly<readonly string[]>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('handles an readonly ReadonlyArray', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = Readonly<ReadonlyArray<string>>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('handles an readonly ReadonlySet', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = Readonly<ReadonlySet<string>>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
// Methods are mutable but arrays have a special exemption; hence no failure. | ||
it('handles a readonly array', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = readonly string[];`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
// Methods are mutable but arrays have a special exemption; hence no failure. | ||
it('handles a ReadonlyArray', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = ReadonlyArray<string>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('treatMethodsAsReadonly', () => { | ||
const options: ReadonlynessOptions = { | ||
treatMethodsAsReadonly: true, | ||
}; | ||
|
||
it('handles a ReadonlySet', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = ReadonlySet<string>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver, options); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('handles a ReadonlyMap', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = ReadonlyMap<string, string>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver, options); | ||
expect(result).toBe(true); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('is not readonly', () => { | ||
describe('default options', () => { | ||
it('fails with record with mutable props', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = { bar: string; };`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('fails with a mutable array', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = string[];`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('fails with a mutable Array', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = Array<string>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
// Methods are mutable; hence failure. | ||
it('fails with a ReadonlySet', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = ReadonlySet<string>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
// Methods are mutable; hence failure. | ||
it('fails with a ReadonlyMap', () => { | ||
const { type: receiver, checker } = getType( | ||
`type Test = ReadonlyMap<string, string>;`, | ||
); | ||
|
||
const result = isTypeReadonly(checker, receiver); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |