Skip to content

test(type-utils): add tests for getDeclaration #10074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/type-utils/tests/getDeclaration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { TSESTree } from '@typescript-eslint/types';
import type { ParserServicesWithTypeInformation } from '@typescript-eslint/typescript-estree';
import type * as ts from 'typescript';

import { getDeclaration } from '../src';

const node = {} as TSESTree.Node;

const mockSymbol = (declarations?: ts.Declaration[]): ts.Symbol => {
return {
getDeclarations: () => declarations,
} as ts.Symbol;
};

const mockServices = (
symbol?: ts.Symbol,
): ParserServicesWithTypeInformation => {
return {
getSymbolAtLocation: (_: TSESTree.Node) => symbol,
} as ParserServicesWithTypeInformation;
};

const mockDeclaration = (): ts.Declaration => {
return {} as ts.Declaration;
};

describe('getDeclaration', () => {
describe('when symbol does not exist', () => {
it('returns null', () => {
const services = mockServices();

expect(getDeclaration(services, node)).toBeNull();
});
});

describe('when declarations do not exist', () => {
it('returns null', () => {
const symbol = mockSymbol();
const services = mockServices(symbol);

expect(getDeclaration(services, node)).toBeNull();
});
});

describe('when declarations exist', () => {
it('returns the first declaration', () => {
const declaration1 = mockDeclaration();
const declaration2 = mockDeclaration();
const symbol = mockSymbol([declaration1, declaration2]);
const services = mockServices(symbol);

expect(getDeclaration(services, node)).toBe(declaration1);
});
});
});
Loading