lion/packages-node/providence-analytics/test-node/program/utils/getSourceCodeFragmentOfDeclaration.test.js

107 lines
3.1 KiB
JavaScript

const { expect } = require('chai');
const { mock } = require('../../../test-helpers/mock-project-helpers.js');
const { getSourceCodeFragmentOfDeclaration } = require('../../../src/program/utils/index.js');
describe('getSourceCodeFragmentOfDeclaration', () => {
describe('Named specifiers', () => {
it('finds source code for directly declared specifiers', async () => {
const fakeFs = {
'/my/proj/exports/file.js': 'export const x = 0;',
};
mock(fakeFs);
const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({
filePath: '/my/proj/exports/file.js',
exportedIdentifier: 'x',
});
expect(sourceFragment).to.equal('0');
});
it('finds source code for referenced specifiers', async () => {
const fakeFs = {
'/my/proj/exports/file.js': `
const y = 0;
export const x = y;
`,
};
mock(fakeFs);
const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({
filePath: '/my/proj/exports/file.js',
exportedIdentifier: 'x',
});
expect(sourceFragment).to.equal('0');
});
it('finds source code for rereferenced specifiers', async () => {
const fakeFs = {
'/my/proj/exports/file.js': `
const x = 88;
const y = x;
export const myIdentifier = y;
`,
};
mock(fakeFs);
const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({
filePath: '/my/proj/exports/file.js',
exportedIdentifier: 'myIdentifier',
});
expect(sourceFragment).to.equal('88');
});
});
describe('[default] specifiers', () => {
it('finds source code for directly declared specifiers', async () => {
const fakeFs = {
'/my/proj/exports/file.js': 'export default class {};',
};
mock(fakeFs);
const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({
filePath: '/my/proj/exports/file.js',
exportedIdentifier: '[default]',
});
expect(sourceFragment).to.equal('class {}');
});
it('finds source code for referenced specifiers', async () => {
const fakeFs = {
'/my/proj/exports/file.js': `
const myIdentifier = 0;
export default myIdentifier;
`,
};
mock(fakeFs);
const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({
filePath: '/my/proj/exports/file.js',
exportedIdentifier: '[default]',
});
expect(sourceFragment).to.equal('0');
});
it('finds source code for rereferenced specifiers', async () => {
const fakeFs = {
'/my/proj/exports/file.js': `
const x = 88;
const myIdentifier = x;
export default myIdentifier;
`,
};
mock(fakeFs);
const { sourceFragment } = await getSourceCodeFragmentOfDeclaration({
filePath: '/my/proj/exports/file.js',
exportedIdentifier: '[default]',
});
expect(sourceFragment).to.equal('88');
});
});
});