|
| 1 | +import {executeBulkOperation} from './execute-bulk-operation.js' |
| 2 | +import {runBulkOperationQuery} from './run-query.js' |
| 3 | +import {AppLinkedInterface} from '../../models/app/app.js' |
| 4 | +import {renderSuccess, renderInfo, renderWarning} from '@shopify/cli-kit/node/ui' |
| 5 | +import {describe, test, expect, vi} from 'vitest' |
| 6 | + |
| 7 | +vi.mock('./run-query.js') |
| 8 | +vi.mock('@shopify/cli-kit/node/ui') |
| 9 | + |
| 10 | +describe('executeBulkOperation', () => { |
| 11 | + const mockApp = { |
| 12 | + name: 'Test App', |
| 13 | + } as AppLinkedInterface |
| 14 | + |
| 15 | + const storeFqdn = 'test-store.myshopify.com' |
| 16 | + const query = 'query { products { edges { node { id } } } }' |
| 17 | + |
| 18 | + const successfulBulkOperation = { |
| 19 | + id: 'gid://shopify/BulkOperation/123', |
| 20 | + status: 'CREATED', |
| 21 | + errorCode: null, |
| 22 | + createdAt: '2024-01-01T00:00:00Z', |
| 23 | + objectCount: '0', |
| 24 | + fileSize: '0', |
| 25 | + url: null, |
| 26 | + } |
| 27 | + |
| 28 | + test('executeBulkOperation successfully runs', async () => { |
| 29 | + const mockResponse = { |
| 30 | + bulkOperation: successfulBulkOperation, |
| 31 | + userErrors: [], |
| 32 | + } |
| 33 | + vi.mocked(runBulkOperationQuery).mockResolvedValue(mockResponse as any) |
| 34 | + |
| 35 | + await executeBulkOperation({ |
| 36 | + app: mockApp, |
| 37 | + storeFqdn, |
| 38 | + query, |
| 39 | + }) |
| 40 | + |
| 41 | + expect(runBulkOperationQuery).toHaveBeenCalledWith({ |
| 42 | + storeFqdn, |
| 43 | + query, |
| 44 | + }) |
| 45 | + |
| 46 | + expect(renderInfo).toHaveBeenCalledWith({ |
| 47 | + headline: 'Starting bulk operation.', |
| 48 | + body: `App: ${mockApp.name}\nStore: ${storeFqdn}`, |
| 49 | + }) |
| 50 | + |
| 51 | + expect(renderInfo).toHaveBeenCalledWith({ |
| 52 | + customSections: expect.arrayContaining([ |
| 53 | + expect.objectContaining({ |
| 54 | + title: 'Bulk Operation Created', |
| 55 | + }), |
| 56 | + ]), |
| 57 | + }) |
| 58 | + |
| 59 | + expect(renderSuccess).toHaveBeenCalledWith({ |
| 60 | + headline: 'Bulk operation started successfully!', |
| 61 | + body: 'Congrats!', |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + test('executeBulkOperation renders warning when user errors are present', async () => { |
| 66 | + const mockResponse = { |
| 67 | + bulkOperation: null, |
| 68 | + userErrors: [ |
| 69 | + {field: ['query'], message: 'Invalid query syntax'}, |
| 70 | + {field: null, message: 'Another error'}, |
| 71 | + ], |
| 72 | + } |
| 73 | + vi.mocked(runBulkOperationQuery).mockResolvedValue(mockResponse as any) |
| 74 | + |
| 75 | + await executeBulkOperation({ |
| 76 | + app: mockApp, |
| 77 | + storeFqdn, |
| 78 | + query, |
| 79 | + }) |
| 80 | + |
| 81 | + expect(renderWarning).toHaveBeenCalledWith({ |
| 82 | + headline: 'Bulk operation errors.', |
| 83 | + body: 'query: Invalid query syntax\nunknown: Another error', |
| 84 | + }) |
| 85 | + |
| 86 | + expect(renderSuccess).not.toHaveBeenCalled() |
| 87 | + }) |
| 88 | +}) |
0 commit comments