Last active
September 23, 2024 13:55
-
-
Save truongluu/d176681478184064c76bde63e28b4177 to your computer and use it in GitHub Desktop.
Mock CSV file in Vitest
This file contains hidden or 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
import { vi, describe, test, expect } from 'vitest'; | |
// Mock CSV content | |
const mockCSVContent = `id,name,age | |
1,John Doe,30 | |
2,Jane Smith,25 | |
3,Bob Johnson,45`; | |
describe('CSV File Object Creation', () => { | |
test('creates a File object with CSV content', () => { | |
// Create a mock File object | |
const mockFile = new File([mockCSVContent], 'test.csv', { type: 'text/csv' }); | |
// Assertions | |
expect(mockFile).toBeInstanceOf(File); | |
expect(mockFile.name).toBe('test.csv'); | |
expect(mockFile.type).toBe('text/csv'); | |
// Read the file content | |
return new Promise((resolve) => { | |
const reader = new FileReader(); | |
reader.onload = (event) => { | |
const content = event.target.result; | |
expect(content).toBe(mockCSVContent); | |
resolve(); | |
}; | |
reader.readAsText(mockFile); | |
}); | |
}); | |
test('simulates file upload with mock CSV File', async () => { | |
const mockFile = new File([mockCSVContent], 'test.csv', { type: 'text/csv' }); | |
// Simulate file upload function | |
const uploadFile = async (file) => { | |
return new Promise((resolve) => { | |
const reader = new FileReader(); | |
reader.onload = (event) => { | |
const content = event.target.result; | |
resolve({ success: true, content }); | |
}; | |
reader.readAsText(file); | |
}); | |
}; | |
const result = await uploadFile(mockFile); | |
expect(result.success).toBe(true); | |
expect(result.content).toBe(mockCSVContent); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment