|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import type { BaseUrl, GraphQLRequestArtifact } from '@/utils/types'; |
| 4 | + |
| 5 | +import { matchGraphQLRequestArtifacts } from './matchGraphQLRequestArtifacts'; |
| 6 | + |
| 7 | +const makeArtifact = (overrides: Partial<GraphQLRequestArtifact>): GraphQLRequestArtifact => |
| 8 | + ({ |
| 9 | + baseUrl: '/' as BaseUrl, |
| 10 | + operationType: 'query', |
| 11 | + config: { data: { ok: true } }, |
| 12 | + weight: 0, |
| 13 | + ...overrides |
| 14 | + }) as GraphQLRequestArtifact; |
| 15 | + |
| 16 | +describe('matchGraphQLRequestArtifacts', () => { |
| 17 | + it('Should not match request path to artifact baseUrl', () => { |
| 18 | + const matched = matchGraphQLRequestArtifacts({ |
| 19 | + artifacts: [makeArtifact({ baseUrl: '/v1', operationName: 'GetUsers' })], |
| 20 | + meta: { |
| 21 | + path: '/v2', |
| 22 | + operationType: 'query', |
| 23 | + operationName: 'GetUsers' |
| 24 | + } |
| 25 | + }); |
| 26 | + expect(matched).toHaveLength(0); |
| 27 | + }); |
| 28 | + |
| 29 | + it('Should return empty when operationType mismatches', () => { |
| 30 | + const matched = matchGraphQLRequestArtifacts({ |
| 31 | + artifacts: [makeArtifact({ operationName: 'GetUsers' })], |
| 32 | + meta: { |
| 33 | + path: '/', |
| 34 | + operationType: 'mutation', |
| 35 | + operationName: 'GetUsers' |
| 36 | + } |
| 37 | + }); |
| 38 | + expect(matched).toHaveLength(0); |
| 39 | + }); |
| 40 | + |
| 41 | + it('Should match equivalent queries with different insignificant whitespace', () => { |
| 42 | + const matched = matchGraphQLRequestArtifacts({ |
| 43 | + artifacts: [makeArtifact({ query: 'query { User { name } }' })], |
| 44 | + meta: { |
| 45 | + path: '/', |
| 46 | + operationType: 'query', |
| 47 | + query: `query { |
| 48 | + User { name } |
| 49 | + }` |
| 50 | + } |
| 51 | + }); |
| 52 | + expect(matched).toHaveLength(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it('Should fail query match when meta query is missing', () => { |
| 56 | + const matched = matchGraphQLRequestArtifacts({ |
| 57 | + artifacts: [makeArtifact({ query: 'query { User { name } }' })], |
| 58 | + meta: { |
| 59 | + path: '/', |
| 60 | + operationType: 'query' |
| 61 | + } |
| 62 | + }); |
| 63 | + expect(matched).toHaveLength(0); |
| 64 | + }); |
| 65 | + |
| 66 | + it('Should fail query match when query strings differ', () => { |
| 67 | + const matched = matchGraphQLRequestArtifacts({ |
| 68 | + artifacts: [makeArtifact({ query: 'query { User { name } }' })], |
| 69 | + meta: { |
| 70 | + path: '/', |
| 71 | + operationType: 'query', |
| 72 | + query: 'query { User { id } }' |
| 73 | + } |
| 74 | + }); |
| 75 | + expect(matched).toHaveLength(0); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Should match operation name string', () => { |
| 79 | + const matched = matchGraphQLRequestArtifacts({ |
| 80 | + artifacts: [makeArtifact({ operationName: 'GetUsers' })], |
| 81 | + meta: { |
| 82 | + path: '/', |
| 83 | + operationType: 'query', |
| 84 | + query: 'query GetUsers { users { id } }', |
| 85 | + operationName: 'GetUsers' |
| 86 | + } |
| 87 | + }); |
| 88 | + expect(matched).toHaveLength(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it('Should fail operation name string when names differ', () => { |
| 92 | + const matched = matchGraphQLRequestArtifacts({ |
| 93 | + artifacts: [makeArtifact({ operationName: 'GetUsers' })], |
| 94 | + meta: { |
| 95 | + path: '/', |
| 96 | + operationType: 'query', |
| 97 | + operationName: 'GetOther' |
| 98 | + } |
| 99 | + }); |
| 100 | + expect(matched).toHaveLength(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Should fail operationName match when meta operation name is missing', () => { |
| 104 | + const matched = matchGraphQLRequestArtifacts({ |
| 105 | + artifacts: [makeArtifact({ operationName: 'GetUsers' })], |
| 106 | + meta: { |
| 107 | + path: '/', |
| 108 | + operationType: 'query', |
| 109 | + query: 'query { users { id } }' |
| 110 | + } |
| 111 | + }); |
| 112 | + expect(matched).toHaveLength(0); |
| 113 | + }); |
| 114 | + |
| 115 | + it('Should match operation name regexp', () => { |
| 116 | + const matched = matchGraphQLRequestArtifacts({ |
| 117 | + artifacts: [makeArtifact({ operationName: /^Get(.+?)sers$/g })], |
| 118 | + meta: { |
| 119 | + path: '/', |
| 120 | + operationType: 'query', |
| 121 | + query: 'query GetUsers { users { id } }', |
| 122 | + operationName: 'GetUsers' |
| 123 | + } |
| 124 | + }); |
| 125 | + expect(matched).toHaveLength(1); |
| 126 | + }); |
| 127 | + |
| 128 | + it('Should match operation name regexp with case-insensitive flag', () => { |
| 129 | + const matched = matchGraphQLRequestArtifacts({ |
| 130 | + artifacts: [makeArtifact({ operationName: /^getusers$/i })], |
| 131 | + meta: { |
| 132 | + path: '/', |
| 133 | + operationType: 'query', |
| 134 | + operationName: 'GetUsers' |
| 135 | + } |
| 136 | + }); |
| 137 | + expect(matched).toHaveLength(1); |
| 138 | + }); |
| 139 | + |
| 140 | + it('Should skip artifact with neither query nor operationName', () => { |
| 141 | + const warn = vi.spyOn(console, 'warn'); |
| 142 | + const artifact = makeArtifact({}); |
| 143 | + |
| 144 | + const matched = matchGraphQLRequestArtifacts({ |
| 145 | + artifacts: [artifact], |
| 146 | + meta: { |
| 147 | + path: '/', |
| 148 | + operationType: 'query', |
| 149 | + operationName: 'GetUsers' |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + expect(matched).toHaveLength(0); |
| 154 | + expect(warn).toHaveBeenCalledWith( |
| 155 | + `[mock-config] GraphQL artifact with no query or operationName was skipped: ${JSON.stringify( |
| 156 | + artifact |
| 157 | + )}` |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + it('Should fail operation name regexp when pattern does not match', () => { |
| 162 | + const matched = matchGraphQLRequestArtifacts({ |
| 163 | + artifacts: [makeArtifact({ operationName: /^Other$/ })], |
| 164 | + meta: { |
| 165 | + path: '/', |
| 166 | + operationType: 'query', |
| 167 | + query: 'query GetUsers { users { id } }', |
| 168 | + operationName: 'GetUsers' |
| 169 | + } |
| 170 | + }); |
| 171 | + expect(matched).toHaveLength(0); |
| 172 | + }); |
| 173 | +}); |
0 commit comments