-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MS-780] feat: More tests. Coverage setup
- Loading branch information
1 parent
4861781
commit 6871a24
Showing
5 changed files
with
122 additions
and
5 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
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
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,97 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { getOperationName } from "./helpers"; | ||
|
||
describe("helpers", () => { | ||
describe("getOperationName", () => { | ||
test("should return the operation name for a query", () => { | ||
const document = ` | ||
query GetUser { | ||
user(id: "1") { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
expect(getOperationName(document)).toBe("GetUser"); | ||
}); | ||
|
||
test("should return the operation name for a mutation", () => { | ||
const document = ` | ||
mutation CreateUser { | ||
createUser(input: { name: "John" }) { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
expect(getOperationName(document)).toBe("CreateUser"); | ||
}); | ||
|
||
test("should return the operation name for a subscription", () => { | ||
const document = ` | ||
subscription OnUserCreated { | ||
userCreated { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
expect(getOperationName(document)).toBe("OnUserCreated"); | ||
}); | ||
|
||
test("should return an empty string if the document has no operation name", () => { | ||
const document = ` | ||
query { | ||
user(id: "1") { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
expect(getOperationName(document)).toBe(""); | ||
}); | ||
|
||
test("should return an empty string for invalid document format", () => { | ||
const document = ` | ||
{ | ||
user(id: "1") { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
expect(getOperationName(document)).toBe(""); | ||
}); | ||
|
||
test("should return an empty string if the operation type is missing", () => { | ||
const document = ` | ||
GetUser { | ||
user(id: "1") { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
expect(getOperationName(document)).toBe(""); | ||
}); | ||
|
||
test.only("should return the correct operation names when there are multiple operations", () => { | ||
const document = ` | ||
query GetUser { | ||
user(id: "1") { | ||
id | ||
name | ||
} | ||
} | ||
mutation CreateUser { | ||
createUser(input: { name: "Jane" }) { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
expect(getOperationName(document)).toBe("GetUser, CreateUser"); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
const OPERATION_NAME_RE = new RegExp( | ||
/[subscription|query|mutation]\s+([^{\s]+)\s*{/ | ||
/[subscription|query|mutation]\s+([^{\s]+)\s*{/g | ||
); | ||
|
||
export const getOperationName = (document: string) => | ||
document.match(OPERATION_NAME_RE)?.[1] ?? ""; | ||
export const getOperationName = (document: string) => { | ||
const matches = [...document.matchAll(OPERATION_NAME_RE)]; | ||
return matches.map((match) => match[1]).join(", "); | ||
}; |
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