|
| 1 | +import {storeExecuteOperation} from './store-execute-operation.js' |
| 2 | +import {resolveApiVersion} from './graphql/common.js' |
| 3 | +import {runGraphQLExecution} from './execute-operation.js' |
| 4 | +import {renderSingleTask} from '@shopify/cli-kit/node/ui' |
| 5 | +import {ensureAuthenticatedAdmin} from '@shopify/cli-kit/node/session' |
| 6 | +import {describe, test, expect, vi, beforeEach} from 'vitest' |
| 7 | + |
| 8 | +vi.mock('./graphql/common.js') |
| 9 | +vi.mock('./execute-operation.js') |
| 10 | +vi.mock('@shopify/cli-kit/node/ui') |
| 11 | +vi.mock('@shopify/cli-kit/node/session') |
| 12 | + |
| 13 | +describe('storeExecuteOperation', () => { |
| 14 | + const storeFqdn = 'test-store.myshopify.com' |
| 15 | + const mockAdminSession = {token: 'user-token', storeFqdn} |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + vi.mocked(ensureAuthenticatedAdmin).mockResolvedValue(mockAdminSession) |
| 19 | + vi.mocked(resolveApiVersion).mockResolvedValue('2024-07') |
| 20 | + vi.mocked(renderSingleTask).mockImplementation(async ({task}) => { |
| 21 | + return task(() => {}) |
| 22 | + }) |
| 23 | + vi.mocked(runGraphQLExecution).mockResolvedValue(undefined) |
| 24 | + }) |
| 25 | + |
| 26 | + test('authenticates as user via ensureAuthenticatedAdmin', async () => { |
| 27 | + await storeExecuteOperation({ |
| 28 | + storeFqdn, |
| 29 | + query: 'query { shop { name } }', |
| 30 | + }) |
| 31 | + |
| 32 | + expect(ensureAuthenticatedAdmin).toHaveBeenCalledWith(storeFqdn) |
| 33 | + }) |
| 34 | + |
| 35 | + test('resolves API version', async () => { |
| 36 | + await storeExecuteOperation({ |
| 37 | + storeFqdn, |
| 38 | + query: 'query { shop { name } }', |
| 39 | + }) |
| 40 | + |
| 41 | + expect(resolveApiVersion).toHaveBeenCalledWith({adminSession: mockAdminSession}) |
| 42 | + }) |
| 43 | + |
| 44 | + test('passes user-specified version to resolveApiVersion', async () => { |
| 45 | + await storeExecuteOperation({ |
| 46 | + storeFqdn, |
| 47 | + query: 'query { shop { name } }', |
| 48 | + version: '2024-01', |
| 49 | + }) |
| 50 | + |
| 51 | + expect(resolveApiVersion).toHaveBeenCalledWith({ |
| 52 | + adminSession: mockAdminSession, |
| 53 | + userSpecifiedVersion: '2024-01', |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + test('delegates to runGraphQLExecution with correct args', async () => { |
| 58 | + await storeExecuteOperation({ |
| 59 | + storeFqdn, |
| 60 | + query: 'query { shop { name } }', |
| 61 | + variables: '{"key":"value"}', |
| 62 | + variableFile: '/path/to/vars.json', |
| 63 | + outputFile: '/path/to/output.json', |
| 64 | + }) |
| 65 | + |
| 66 | + expect(runGraphQLExecution).toHaveBeenCalledWith({ |
| 67 | + adminSession: mockAdminSession, |
| 68 | + query: 'query { shop { name } }', |
| 69 | + variables: '{"key":"value"}', |
| 70 | + variableFile: '/path/to/vars.json', |
| 71 | + outputFile: '/path/to/output.json', |
| 72 | + version: '2024-07', |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + test('passes undefined optional fields when not provided', async () => { |
| 77 | + await storeExecuteOperation({ |
| 78 | + storeFqdn, |
| 79 | + query: 'query { shop { name } }', |
| 80 | + }) |
| 81 | + |
| 82 | + expect(runGraphQLExecution).toHaveBeenCalledWith({ |
| 83 | + adminSession: mockAdminSession, |
| 84 | + query: 'query { shop { name } }', |
| 85 | + variables: undefined, |
| 86 | + variableFile: undefined, |
| 87 | + outputFile: undefined, |
| 88 | + version: '2024-07', |
| 89 | + }) |
| 90 | + }) |
| 91 | +}) |
0 commit comments