Skip to content

Commit

Permalink
test(decorators): add test case for nullify custom method decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Feb 1, 2019
1 parent 3e5ea9d commit a86432c
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions tests/functional/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,14 +1082,27 @@ describe("Resolvers", () => {
let queryRoot: any;
let queryContext: any;
let queryInfo: any;
let descriptorEvaluated: boolean;

function DescriptorDecorator(): MethodDecorator {
return (obj, methodName, descriptor: any) => {
const originalMethod: Function = descriptor.value;
descriptor.value = function() {
descriptorEvaluated = true;
return originalMethod.apply(this, arguments);
};
};
}

// helpers
function generateAndVisitComplexMethod(maximumComplexity: number): ValidationContext {
const query = `query {
sampleQuery {
complexResolverMethod
}
}`;
const query = /* graphql */ `
query {
sampleQuery {
complexResolverMethod
}
}
`;
const ast = parse(query);
const typeInfo = new TypeInfo(schema);
const context = new ValidationContext(schema, ast, typeInfo);
Expand All @@ -1105,6 +1118,7 @@ describe("Resolvers", () => {
queryRoot = undefined;
queryContext = undefined;
queryInfo = undefined;
descriptorEvaluated = false;
});

beforeAll(async () => {
Expand Down Expand Up @@ -1216,6 +1230,12 @@ describe("Resolvers", () => {
return true;
}

@Query()
@DescriptorDecorator()
queryWithCustomDescriptorDecorator(): boolean {
return true;
}

@Mutation()
mutationWithArgs(@Args() args: SampleArgs): number {
if (args.isTrue()) {
Expand Down Expand Up @@ -1507,6 +1527,19 @@ describe("Resolvers", () => {
expect(queryRoot).toEqual(2);
expect(queryContext).toEqual("present");
});

it("should allow for overwriting descriptor value in custom decorator", async () => {
const query = /* graphql */ `
query {
queryWithCustomDescriptorDecorator
}
`;

const { data } = await graphql(schema, query);

expect(descriptorEvaluated).toBe(true);
expect(data.queryWithCustomDescriptorDecorator).toBe(true);
});
});

describe("buildSchema", async () => {
Expand Down

0 comments on commit a86432c

Please sign in to comment.