|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { graphql, parse } from 'graphql'; |
| 3 | +import { makeExecutableSchema } from '@graphql-tools/schema'; |
| 4 | +import { makeFeatureFlagDirectiveTransformer } from '../../../src/graphql/featureFlagDirective'; |
| 5 | +import { isFeatureEnabled } from '../../../src/config/conf'; |
| 6 | + |
| 7 | +vi.mock('../../../src/config/conf', () => ({ |
| 8 | + isFeatureEnabled: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +describe('featureFlagDirective', () => { |
| 12 | + afterEach(() => { |
| 13 | + vi.resetAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('returns a Forbidden error when none of the flags are enabled', async () => { |
| 17 | + const typeDefs = parse(` |
| 18 | + directive @ff(flags: [String!]!, softFail: Boolean = false) on FIELD_DEFINITION |
| 19 | + type Query { |
| 20 | + flaggedFeature: String @ff(flags: ["SOME_FLAG", "SOME_OTHER_FLAG"]) |
| 21 | + } |
| 22 | + `); |
| 23 | + const resolvers = { |
| 24 | + Query: { |
| 25 | + flaggedFeature: () => 'experimental content', |
| 26 | + }, |
| 27 | + }; |
| 28 | + vi.mocked(isFeatureEnabled).mockReturnValue(false); |
| 29 | + |
| 30 | + let schema = makeExecutableSchema({ typeDefs, resolvers }); |
| 31 | + schema = makeFeatureFlagDirectiveTransformer()(schema); |
| 32 | + |
| 33 | + const result = await graphql({ schema, source: '{ flaggedFeature }' }); |
| 34 | + |
| 35 | + expect(result.errors).not.toBeUndefined(); |
| 36 | + expect(result.errors?.[0].message).toMatch(/You are not allowed to do this/i); |
| 37 | + expect(result.errors?.[0].extensions?.code).toMatch(/FORBIDDEN_ACCESS/i); |
| 38 | + }); |
| 39 | + |
| 40 | + it('calls the resolver when one of the flags is enabled', async () => { |
| 41 | + const typeDefs = parse(` |
| 42 | + directive @ff(flags: [String!]!, softFail: Boolean = false) on FIELD_DEFINITION |
| 43 | + type Query { |
| 44 | + flaggedFeature: String @ff(flags: ["SOME_FLAG", "SOME_OTHER_FLAG"]) |
| 45 | + } |
| 46 | + `); |
| 47 | + const resolvers = { |
| 48 | + Query: { |
| 49 | + flaggedFeature: () => 'experimental content', |
| 50 | + }, |
| 51 | + }; |
| 52 | + vi.mocked(isFeatureEnabled).mockImplementation((flag: string) => { |
| 53 | + return flag === 'SOME_FLAG'; |
| 54 | + }); |
| 55 | + |
| 56 | + let schema = makeExecutableSchema({ typeDefs, resolvers }); |
| 57 | + schema = makeFeatureFlagDirectiveTransformer()(schema); |
| 58 | + |
| 59 | + const result = await graphql({ schema, source: '{ flaggedFeature }' }); |
| 60 | + |
| 61 | + expect(result.data?.flaggedFeature).toBe('experimental content'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns null when none of the flags are enabled and softFail is set', async () => { |
| 65 | + const typeDefs = parse(` |
| 66 | + directive @ff(flags: [String!]!, softFail: Boolean = false) on FIELD_DEFINITION |
| 67 | + type Query { |
| 68 | + flaggedFeature: String @ff(flags: ["SOME_FLAG"], softFail: true) |
| 69 | + } |
| 70 | + `); |
| 71 | + const resolvers = { |
| 72 | + Query: { |
| 73 | + flaggedFeature: () => 'experimental content', |
| 74 | + }, |
| 75 | + }; |
| 76 | + vi.mocked(isFeatureEnabled).mockReturnValue(false); |
| 77 | + |
| 78 | + let schema = makeExecutableSchema({ typeDefs, resolvers }); |
| 79 | + schema = makeFeatureFlagDirectiveTransformer()(schema); |
| 80 | + |
| 81 | + const result = await graphql({ schema, source: '{ flaggedFeature }' }); |
| 82 | + |
| 83 | + expect(result.errors).toBeUndefined(); |
| 84 | + expect(result.data?.flaggedFeature).toBeNull(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments