Skip to content

Commit

Permalink
Rewrite find references tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Aug 24, 2024
1 parent c2e54b9 commit 117ce52
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 45 deletions.
124 changes: 124 additions & 0 deletions packages/language-server/tests/__snapshots__/completions.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Completions > #2454 1`] = `
"
<script setup lang="ts">
let vLoading: any;
</script>
<template>
<div v-loading="vLoading"></div>
</template>
"
`;

exports[`Completions > #2511 1`] = `
"
<script setup lang="ts">
import componentForAutoImport$1 from './component-for-auto-import.vue';
</script>
"
`;

exports[`Completions > #3658 1`] = `
"
<template>
<Comp>
<template #foo="foo">
{{ foo }}
</template>
</Comp>
</template>
"
`;
exports[`Completions > #4639 1`] = `
"
<template>
<div @click.capture />
</template>
"
`;
exports[`Completions > $event argument 1`] = `"<template><div @click="console.log($event)"></div></template>"`;
exports[`Completions > <script setup> 1`] = `
"
<template>{{ foo }}</template>
<script lang="ts" setup>
const foo = 1;
</script>
"
`;
exports[`Completions > Alias path 1`] = `
"
<script setup lang="ts">
import Component from '@/empty.vue';
</script>
"
`;
exports[`Completions > Component auto import 1`] = `
"
<script setup lang="ts">
import ComponentForAutoImport from './ComponentForAutoImport.vue';
</script>
<template>
<ComponentForAutoImport />
</template>
"
`;
exports[`Completions > Directives 1`] = `"<template><div v-html="$1"></div></template>"`;
exports[`Completions > Directives 2`] = `"<template><div v-cloak></div></template>"`;
exports[`Completions > Directives 3`] = `"<template><div v-else></div></template>"`;
exports[`Completions > Directives 4`] = `"<template><div v-pre></div></template>"`;
exports[`Completions > Relative path 1`] = `
"
<script setup lang="ts">
import Component from './empty.vue';
</script>
"
`;
exports[`Completions > Slot name 1`] = `
"
<template>
<Foo>
<template #default></template>
</Foo>
</template>
<script lang="ts" setup>
let Foo: new () => {
$slots: {
default: any;
};
};
</script>
"
`;
exports[`Completions > core#8811 1`] = `
"
<script setup lang="ts">
declare const Foo: new () => {
$props: {
FooBar: string;
};
};
</script>
<template>
<Foo :-foo-bar="$1" ></Foo>
</template>
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Definitions > Default slot 1`] = `"tsconfigProject/foo.vue"`;

exports[`Definitions > Default slot 2`] = `
{
"end": {
"character": 16,
"line": 7,
},
"start": {
"character": 5,
"line": 7,
},
}
`;

exports[`Definitions > Default slot 3`] = `"tsconfigProject/fixture.vue"`;

exports[`Definitions > Default slot 4`] = `
{
"end": {
"character": 9,
"line": 2,
},
"start": {
"character": 5,
"line": 2,
},
}
`;

exports[`Definitions > Named slot 1`] = `"tsconfigProject/foo.vue"`;

exports[`Definitions > Named slot 2`] = `
{
"end": {
"character": 17,
"line": 6,
},
"start": {
"character": 14,
"line": 6,
},
}
`;

exports[`Definitions > Named slot 3`] = `"tsconfigProject/fixture.vue"`;

exports[`Definitions > Named slot 4`] = `
{
"end": {
"character": 19,
"line": 2,
},
"start": {
"character": 16,
"line": 2,
},
}
`;

exports[`Definitions > v-bind shorthand 1`] = `"tsconfigProject/fixture.vue"`;

exports[`Definitions > v-bind shorthand 2`] = `
{
"end": {
"character": 13,
"line": 6,
},
"start": {
"character": 10,
"line": 6,
},
}
`;

exports[`Definitions > v-bind shorthand 3`] = `"tsconfigProject/fixture.vue"`;

exports[`Definitions > v-bind shorthand 4`] = `
{
"end": {
"character": 12,
"line": 2,
},
"start": {
"character": 9,
"line": 2,
},
}
`;
106 changes: 106 additions & 0 deletions packages/language-server/tests/references.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { TextDocument } from '@volar/language-server';
import * as path from 'path';
import { afterEach, describe, expect, it } from 'vitest';
import { URI } from 'vscode-uri';
import { getLanguageServer, testWorkspacePath } from './server.js';

describe('Definitions', async () => {

it('Default slot', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await openDocument('tsconfigProject/foo.vue', 'vue', `
<script setup lang="ts">
import Fixture from './fixture.vue';
</script>
<template>
<Fixture>
<div></div>
</Fixture>
</template>
`);
await assertReferences('tsconfigProject/fixture.vue', 'vue', `
<template>
<slot|></slot>
</template>
`);
});

it('Named slot', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await openDocument('tsconfigProject/foo.vue', 'vue', `
<script setup lang="ts">
import Fixture from './fixture.vue';
</script>
<template>
<Fixture #foo></Fixture>
</template>
`);
await assertReferences('tsconfigProject/fixture.vue', 'vue', `
<template>
<slot name="|foo"></slot>
</template>
`);
});

it('v-bind shorthand', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await assertReferences('tsconfigProject/fixture.vue', 'vue', `
<script setup lang="ts">
const |foo = 1;
</script>
<template>
<Foo :foo></Foo>
</template>
`);
});

const openedDocuments: TextDocument[] = [];

afterEach(async () => {
const server = await getLanguageServer();
for (const document of openedDocuments) {
await server.closeTextDocument(document.uri);
}
openedDocuments.length = 0;
});

/**
* @deprecated Remove this when #4717 fixed.
*/
async function ensureGlobalTypesHolder(folderName: string) {
const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', '');
const server = await getLanguageServer();
await server.sendDocumentDiagnosticRequest(document.uri);
}

async function assertReferences(fileName: string, languageId: string, content: string) {
const offset = content.indexOf('|');
content = content.slice(0, offset) + content.slice(offset + 1);

const server = await getLanguageServer();
let document = await openDocument(fileName, languageId, content);

const position = document.positionAt(offset);
const references = await server.sendReferencesRequest(document.uri, position, { includeDeclaration: false });
expect(references).toBeDefined();
expect(references!.length).greaterThan(0);

for (const loc of references!) {
expect(path.relative(testWorkspacePath, URI.parse(loc.uri).fsPath)).toMatchSnapshot();

Check failure on line 92 in packages/language-server/tests/references.spec.ts

View workflow job for this annotation

GitHub Actions / build (18, windows-latest)

packages/language-server/tests/references.spec.ts > Definitions > Default slot

Error: Snapshot `Definitions > Default slot 1` mismatched - Expected + Received - "tsconfigProject/foo.vue" + "tsconfigProject\foo.vue" ❯ assertReferences packages/language-server/tests/references.spec.ts:92:72 ❯ packages/language-server/tests/references.spec.ts:22:3

Check failure on line 92 in packages/language-server/tests/references.spec.ts

View workflow job for this annotation

GitHub Actions / build (18, windows-latest)

packages/language-server/tests/references.spec.ts > Definitions > Named slot

Error: Snapshot `Definitions > Named slot 1` mismatched - Expected + Received - "tsconfigProject/foo.vue" + "tsconfigProject\foo.vue" ❯ assertReferences packages/language-server/tests/references.spec.ts:92:72 ❯ packages/language-server/tests/references.spec.ts:40:3

Check failure on line 92 in packages/language-server/tests/references.spec.ts

View workflow job for this annotation

GitHub Actions / build (18, windows-latest)

packages/language-server/tests/references.spec.ts > Definitions > v-bind shorthand

Error: Snapshot `Definitions > v-bind shorthand 1` mismatched - Expected + Received - "tsconfigProject/fixture.vue" + "tsconfigProject\fixture.vue" ❯ assertReferences packages/language-server/tests/references.spec.ts:92:72 ❯ packages/language-server/tests/references.spec.ts:49:3
expect(loc.range).toMatchSnapshot();
}
}

async function openDocument(fileName: string, languageId: string, content: string) {
const server = await getLanguageServer();
const uri = URI.file(`${testWorkspacePath}/${fileName}`);
const document = await server.openInMemoryDocument(uri.toString(), languageId, content);
if (openedDocuments.every(d => d.uri !== document.uri)) {
openedDocuments.push(document);
}
return document;
}
});

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions test-workspace/language-service/reference/slot-named/foo.vue

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 3 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
include: ['packages/language-server/tests/**/*.spec.ts'],
poolOptions: {
threads: {
singleThread: true,
forks: {
singleFork: true,
isolate: false,
},
},
Expand Down

0 comments on commit 117ce52

Please sign in to comment.