-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cd0ae0
commit 32f4ddb
Showing
5 changed files
with
210 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/attachments/tests/attachments/AttachmentQueue.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as commonSdk from '@powersync/common'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { AbstractAttachmentQueue } from '../../src/AbstractAttachmentQueue'; | ||
import { AttachmentRecord, AttachmentState } from '../../src/Schema'; | ||
import { AbstractPowerSyncDatabase } from '@powersync/common'; | ||
import { StorageAdapter } from '../../src/StorageAdapter'; | ||
|
||
const record = { | ||
id: 'test-1', | ||
filename: 'test.jpg', | ||
state: AttachmentState.QUEUED_DOWNLOAD | ||
} | ||
|
||
const mockPowerSync = { | ||
currentStatus: { status: 'initial' }, | ||
registerListener: vi.fn(() => {}), | ||
resolveTables: vi.fn(() => ['table1', 'table2']), | ||
onChangeWithCallback: vi.fn(), | ||
getAll: vi.fn(() => Promise.resolve([{id: 'test-1'}, {id: 'test-2'}])), | ||
execute: vi.fn(() => Promise.resolve()), | ||
getOptional: vi.fn((_query, params) => Promise.resolve(record)), | ||
watch: vi.fn((query, params, callbacks) => { | ||
callbacks?.onResult?.({ rows: { _array: [{id: 'test-1'}, {id: 'test-2'}] } }); | ||
}), | ||
writeTransaction: vi.fn(async (callback) => { | ||
await callback({ | ||
execute: vi.fn(() => Promise.resolve()) | ||
}); | ||
}) | ||
}; | ||
|
||
const mockStorage: StorageAdapter = { | ||
downloadFile: vi.fn(), | ||
uploadFile: vi.fn(), | ||
deleteFile: vi.fn(), | ||
writeFile: vi.fn(), | ||
readFile: vi.fn(), | ||
fileExists: vi.fn(), | ||
makeDir: vi.fn(), | ||
copyFile: vi.fn(), | ||
getUserStorageDirectory: vi.fn() | ||
}; | ||
|
||
class TestAttachmentQueue extends AbstractAttachmentQueue { | ||
onAttachmentIdsChange(onUpdate: (ids: string[]) => void): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
newAttachmentRecord(record?: Partial<AttachmentRecord>): Promise<AttachmentRecord> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} | ||
|
||
describe('attachments', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should not download attachments when downloadRecord is called with downloadAttachments false', async () => { | ||
const queue = new TestAttachmentQueue({ | ||
powersync: mockPowerSync as any, | ||
storage: mockStorage, | ||
downloadAttachments: false | ||
}); | ||
|
||
await queue.downloadRecord(record); | ||
|
||
expect(mockStorage.downloadFile).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should download attachments when downloadRecord is called with downloadAttachments true', async () => { | ||
const queue = new TestAttachmentQueue({ | ||
powersync: mockPowerSync as any, | ||
storage: mockStorage, | ||
downloadAttachments: true | ||
}); | ||
|
||
await queue.downloadRecord(record); | ||
|
||
expect(mockStorage.downloadFile).toHaveBeenCalled(); | ||
}); | ||
|
||
// Testing the inverse of this test, i.e. when downloadAttachments is false, is not required as you can't wait for something that does not happen | ||
it('should not download attachments with watchDownloads is called with downloadAttachments false', async () => { | ||
const queue = new TestAttachmentQueue({ | ||
powersync: mockPowerSync as any, | ||
storage: mockStorage, | ||
downloadAttachments: true | ||
}); | ||
|
||
queue.watchDownloads(); | ||
await vi.waitFor(() => { | ||
expect(mockStorage.downloadFile).toBeCalledTimes(2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import wasm from 'vite-plugin-wasm'; | ||
import topLevelAwait from 'vite-plugin-top-level-await'; | ||
import { defineConfig, UserConfigExport } from 'vitest/config'; | ||
|
||
const config: UserConfigExport = { | ||
worker: { | ||
format: 'es', | ||
plugins: () => [wasm(), topLevelAwait()] | ||
}, | ||
optimizeDeps: { | ||
// Don't optimise these packages as they contain web workers and WASM files. | ||
// https://github.com/vitejs/vite/issues/11672#issuecomment-1415820673 | ||
exclude: ['@journeyapps/wa-sqlite', '@powersync/web'] | ||
}, | ||
plugins: [wasm(), topLevelAwait()], | ||
test: { | ||
isolate: false, | ||
globals: true, | ||
include: ['tests/**/*.test.ts'], | ||
browser: { | ||
enabled: true, | ||
headless: true, | ||
provider: 'webdriverio', | ||
name: 'chrome' // browser name is required | ||
} | ||
} | ||
}; | ||
|
||
export default defineConfig(config); |
Oops, something went wrong.