Skip to content

Commit

Permalink
Merge pull request #12 from rintoj/feat/definition-provider-enhancements
Browse files Browse the repository at this point in the history
Enhance Definition Provider with Additional Test Cases and Enum Support
  • Loading branch information
rintoj authored Jul 9, 2024
2 parents 279678d + 56e9ca3 commit 0c39881
Show file tree
Hide file tree
Showing 15 changed files with 910 additions and 106 deletions.
98 changes: 91 additions & 7 deletions src/definition-provider/definition-provider-for-schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { resolve } from 'path'
import { Position, Range } from '../diff'
import { trimSpaces } from '../util/trim-spaces'
import { provideDefinitionForSchema } from './definition-provider-for-schema'
import { readFileSync } from 'fs-extra'

const schema = `
type User {
Expand Down Expand Up @@ -32,9 +34,15 @@ used as primitive type
"""
type Tweet {
tweetId: ID!
status: TweetStatus
mentions: [User!]
}
enum TweetStatus {
DRAFT
ACTIVE
}
type Query {
me: User
user(id: ID!): User
Expand All @@ -49,7 +57,6 @@ type Mutation {
type Subscription {
onUserChange(id: ID!): User
}
`

function getAt(schema: string, range: Range | null) {
Expand All @@ -58,12 +65,31 @@ function getAt(schema: string, range: Range | null) {
return lines.join('\n')
}

async function process(position: Position) {
const foundPosition = await provideDefinitionForSchema(
schema,
'schema.gql',
position,
resolve(__dirname, 'test', '*.resolver.ts'),
resolve(__dirname, 'test', '*.model.ts'),
resolve(__dirname, 'test', '*.enum.ts'),
)
if (!foundPosition) return ''
const source = foundPosition.path.startsWith('schema.gql')
? schema
: readFileSync(foundPosition.path, 'utf-8')
return [
`# ${[foundPosition.path.replace(__dirname + '/', ''), foundPosition.range.start.line, foundPosition.range.start.character].join(':')}`,
getAt(source, foundPosition.range),
].join('\n')
}

describe('provideDefinitionFromSchema', () => {
test('should provide return address type', async () => {
const range = provideDefinitionForSchema(schema, new Position(4, 16))
const output = getAt(schema, range)
const output = await process(new Position(4, 16))
expect(output).toEqual(
trimSpaces(`
# schema.gql:13:0
type Address {
id: String
address: String
Expand All @@ -73,10 +99,10 @@ describe('provideDefinitionFromSchema', () => {
})

test('should provide user status', async () => {
const range = provideDefinitionForSchema(schema, new Position(5, 16))
const output = getAt(schema, range)
const output = await process(new Position(5, 16))
expect(output).toEqual(
trimSpaces(`
# schema.gql:8:0
enum UserStatus {
ACTIVE,
DELETED
Expand All @@ -85,14 +111,72 @@ describe('provideDefinitionFromSchema', () => {
})

test('should provide tweet type', async () => {
const range = provideDefinitionForSchema(schema, new Position(36, 22))
const output = getAt(schema, range)
const output = await process(new Position(42, 22))
expect(output).toEqual(
trimSpaces(`
# schema.gql:28:0
type Tweet {
tweetId: ID!
status: TweetStatus
mentions: [User!]
}`),
)
})

test('should provide tweet status', async () => {
const output = await process(new Position(30, 22))
expect(output).toEqual(
trimSpaces(`
# schema.gql:34:0
enum TweetStatus {
DRAFT
ACTIVE
}`),
)
})

test('should provide definition tweet type in typescript', async () => {
const output = await process(new Position(42, 6))
expect(output).toEqual(
trimSpaces(`
# test/tweet.resolver.ts:6:2
tweet() {`),
)
})

test('should provide definition user.name in typescript', async () => {
const output = await process(new Position(3, 5))
expect(output).toEqual(
trimSpaces(`
# test/user.model.ts:6:2
name?: string`),
)
})

test('should provide definition user id in typescript', async () => {
const output = await process(new Position(2, 3))
expect(output).toEqual(
trimSpaces(`
# test/base.model.ts:5:2
id!: string`),
)
})

test('should provide definition of enum in typescript', async () => {
const output = await process(new Position(34, 12))
expect(output).toEqual(
trimSpaces(`
# test/tweet-status.enum.ts:2:12
export enum TweetStatus {`),
)
})

test('should provide definition of enum member in typescript', async () => {
const output = await process(new Position(35, 6))
expect(output).toEqual(
trimSpaces(`
# test/tweet-status.enum.ts:3:2
DRAFT = 'DRAFT',`),
)
})
})
Loading

0 comments on commit 0c39881

Please sign in to comment.