|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { Mock } from 'vitest'; |
| 3 | +import type { Attachment } from '@wordpress/core-data'; |
| 4 | +import { dispatch } from '@wordpress/data'; |
| 5 | +import { |
| 6 | + receiveFinalizedAttachment, |
| 7 | + consumeFinalizedAttachment, |
| 8 | +} from '../finalized-attachments'; |
| 9 | + |
| 10 | +vi.mock( import( '@wordpress/data' ), () => ( { |
| 11 | + dispatch: vi.fn(), |
| 12 | +} ) ); |
| 13 | + |
| 14 | +vi.mock( |
| 15 | + import( '@wordpress/core-data' ), |
| 16 | + () => |
| 17 | + ( { |
| 18 | + store: 'core', |
| 19 | + } ) as unknown as typeof import('@wordpress/core-data') |
| 20 | +); |
| 21 | + |
| 22 | +const receiveEntityRecords = vi.fn(); |
| 23 | + |
| 24 | +describe( 'receiveFinalizedAttachment', () => { |
| 25 | + beforeEach( () => { |
| 26 | + vi.clearAllMocks(); |
| 27 | + ( dispatch as unknown as Mock ).mockReturnValue( { |
| 28 | + receiveEntityRecords, |
| 29 | + } ); |
| 30 | + } ); |
| 31 | + |
| 32 | + afterEach( () => { |
| 33 | + // Leave no entry behind for the next test. |
| 34 | + consumeFinalizedAttachment( 123 ); |
| 35 | + } ); |
| 36 | + |
| 37 | + it( 'receives the record under both queries the editor resolves attachments with', () => { |
| 38 | + const record: Partial< Attachment > = { |
| 39 | + id: 123, |
| 40 | + media_details: { |
| 41 | + sizes: { |
| 42 | + full: { |
| 43 | + file: 'image.jpg', |
| 44 | + width: 1024, |
| 45 | + height: 768, |
| 46 | + mime_type: 'image/jpeg', |
| 47 | + source_url: 'https://example.com/image.jpg', |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + receiveFinalizedAttachment( record ); |
| 54 | + |
| 55 | + expect( receiveEntityRecords ).toHaveBeenCalledTimes( 2 ); |
| 56 | + expect( receiveEntityRecords ).toHaveBeenCalledWith( |
| 57 | + 'postType', |
| 58 | + 'attachment', |
| 59 | + record, |
| 60 | + { context: 'view' } |
| 61 | + ); |
| 62 | + expect( receiveEntityRecords ).toHaveBeenCalledWith( |
| 63 | + 'postType', |
| 64 | + 'attachment', |
| 65 | + record, |
| 66 | + undefined |
| 67 | + ); |
| 68 | + } ); |
| 69 | + |
| 70 | + it( 'receives the record on its own, so list queries are left alone', () => { |
| 71 | + // core-data only merges an item list into a query's results when the |
| 72 | + // received items are an array; a lone record must stay a lone record. |
| 73 | + receiveFinalizedAttachment( { id: 123 } ); |
| 74 | + |
| 75 | + for ( const call of receiveEntityRecords.mock.calls ) { |
| 76 | + expect( Array.isArray( call[ 2 ] ) ).toBe( false ); |
| 77 | + } |
| 78 | + } ); |
| 79 | + |
| 80 | + it( 'ignores a response without an ID', () => { |
| 81 | + receiveFinalizedAttachment( {} ); |
| 82 | + receiveFinalizedAttachment( |
| 83 | + undefined as unknown as Partial< Attachment > |
| 84 | + ); |
| 85 | + |
| 86 | + expect( receiveEntityRecords ).not.toHaveBeenCalled(); |
| 87 | + } ); |
| 88 | +} ); |
| 89 | + |
| 90 | +describe( 'consumeFinalizedAttachment', () => { |
| 91 | + beforeEach( () => { |
| 92 | + vi.clearAllMocks(); |
| 93 | + ( dispatch as unknown as Mock ).mockReturnValue( { |
| 94 | + receiveEntityRecords, |
| 95 | + } ); |
| 96 | + } ); |
| 97 | + |
| 98 | + it( 'reports an attachment whose finalized record was received', () => { |
| 99 | + receiveFinalizedAttachment( { id: 456 } ); |
| 100 | + |
| 101 | + expect( consumeFinalizedAttachment( 456 ) ).toBe( true ); |
| 102 | + } ); |
| 103 | + |
| 104 | + it( 'reports an attachment that was never finalized', () => { |
| 105 | + expect( consumeFinalizedAttachment( 789 ) ).toBe( false ); |
| 106 | + } ); |
| 107 | + |
| 108 | + it( 'forgets the attachment once consumed, so a later upload is not skipped', () => { |
| 109 | + receiveFinalizedAttachment( { id: 456 } ); |
| 110 | + |
| 111 | + expect( consumeFinalizedAttachment( 456 ) ).toBe( true ); |
| 112 | + expect( consumeFinalizedAttachment( 456 ) ).toBe( false ); |
| 113 | + } ); |
| 114 | +} ); |
0 commit comments