Skip to content

Commit

Permalink
Negative unit tests for dynamic cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmcgivery committed Aug 6, 2024
1 parent 53046a1 commit af4dd27
Showing 1 changed file with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('dynamic cache control', () => {
expect(hints).toStrictEqual(new Map([['droid', { maxAge: 60 }]]));
});

it('should set the maxAge for a field from a dynamic cache hint using a human-readable format', async () => {
it('should set the maxAge for a field from a dynamic cache hint when using a human-readable format', async () => {
const typeDefs = `
type Query {
droid(id: ID!): Droid
Expand Down Expand Up @@ -114,6 +114,50 @@ describe('dynamic cache control', () => {
expect(hints).toStrictEqual(new Map([['droid', { maxAge: 86400 }]]));
});

it('should not set the maxAge for a field from a dynamic cache hint when using am invalid human-readable format', async () => {
const typeDefs = `
type Query {
droid(id: ID!): Droid
}
type Droid {
id: ID!
name: String!
}
`;

const resolvers: GraphQLResolvers = {
Query: {
droid: (_source, _args, _context, info) => {
cacheControlFromInfo(info).setCacheHint({ maxAge: '1ddddd' });
return {
id: 2001,
name: 'R2-D2',
};
},
},
};

const schema = makeExecutableSchemaWithCacheControlSupport({
typeDefs,
resolvers,
});

const hints = await collectCacheControlHints(
schema,
`
query {
droid(id: 2001) {
name
}
}
`,
{ defaultMaxAge: 10 },
);

expect(hints).toStrictEqual(new Map([['droid', { maxAge: 10 }]]));
});

it('should set the max age of the field when cache hint is restricted', async () => {
const typeDefs = `
type Query {
Expand Down Expand Up @@ -202,6 +246,50 @@ describe('dynamic cache control', () => {
expect(hints).toStrictEqual(new Map([['droid', { maxAge: 86400 }]]));
});

it('should not set the max age of the field when cache hint is restricted with an invalid human readable format', async () => {
const typeDefs = `
type Query {
droid(id: ID!): Droid
}
type Droid {
id: ID!
name: String!
}
`;

const resolvers: GraphQLResolvers = {
Query: {
droid: (_source, _args, _context, info) => {
cacheControlFromInfo(info).cacheHint.restrict({ maxAge: '1ddddd' });
return {
id: 2001,
name: 'R2-D2',
};
},
},
};

const schema = makeExecutableSchemaWithCacheControlSupport({
typeDefs,
resolvers,
});

const hints = await collectCacheControlHints(
schema,
`
query {
droid(id: 2001) {
name
}
}
`,
{ defaultMaxAge: 10 },
);

expect(hints).toStrictEqual(new Map([['droid', { maxAge: 10 }]]));
});

it('should set the scope for a field from a dynamic cache hint', async () => {
const typeDefs = `
type Query {
Expand Down

0 comments on commit af4dd27

Please sign in to comment.