|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +/** |
| 4 | + * MSW Verification Test |
| 5 | + * Verifies that MSW is properly mocking API calls without requiring a backend |
| 6 | + */ |
| 7 | + |
| 8 | +test.describe('MSW API Mocking Verification', () => { |
| 9 | + test('should mock API responses successfully', async ({ page }) => { |
| 10 | + // Mock API endpoint |
| 11 | + await page.route('**/api/test', async (route) => { |
| 12 | + await route.fulfill({ |
| 13 | + status: 200, |
| 14 | + contentType: 'application/json', |
| 15 | + body: JSON.stringify({ |
| 16 | + success: true, |
| 17 | + message: 'MSW is working', |
| 18 | + data: { test: 'value' }, |
| 19 | + }), |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + // Create a test page that makes an API call |
| 24 | + await page.setContent(` |
| 25 | + <!DOCTYPE html> |
| 26 | + <html> |
| 27 | + <head><title>MSW Test</title></head> |
| 28 | + <body> |
| 29 | + <div id="result">Loading...</div> |
| 30 | + <script> |
| 31 | + fetch('/api/test') |
| 32 | + .then(res => res.json()) |
| 33 | + .then(data => { |
| 34 | + document.getElementById('result').textContent = data.message; |
| 35 | + }) |
| 36 | + .catch(err => { |
| 37 | + document.getElementById('result').textContent = 'Error: ' + err.message; |
| 38 | + }); |
| 39 | + </script> |
| 40 | + </body> |
| 41 | + </html> |
| 42 | + `); |
| 43 | + |
| 44 | + // Wait for the API call to complete |
| 45 | + await page.waitForTimeout(1000); |
| 46 | + |
| 47 | + // Verify the mocked response was used |
| 48 | + const result = await page.locator('#result').textContent(); |
| 49 | + expect(result).toBe('MSW is working'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should mock property API endpoints', async ({ page }) => { |
| 53 | + // Mock property listings endpoint |
| 54 | + await page.route('**/api/properties', async (route) => { |
| 55 | + await route.fulfill({ |
| 56 | + status: 200, |
| 57 | + contentType: 'application/json', |
| 58 | + body: JSON.stringify({ |
| 59 | + properties: [ |
| 60 | + { |
| 61 | + id: 'test-1', |
| 62 | + name: 'Test Property', |
| 63 | + price: { total: 1000000, perToken: 100, currency: 'USD' }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + total: 1, |
| 67 | + page: 1, |
| 68 | + totalPages: 1, |
| 69 | + }), |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + // Create a test page |
| 74 | + await page.setContent(` |
| 75 | + <!DOCTYPE html> |
| 76 | + <html> |
| 77 | + <head><title>Property Test</title></head> |
| 78 | + <body> |
| 79 | + <div id="property-count">0</div> |
| 80 | + <div id="property-name"></div> |
| 81 | + <script> |
| 82 | + fetch('/api/properties') |
| 83 | + .then(res => res.json()) |
| 84 | + .then(data => { |
| 85 | + document.getElementById('property-count').textContent = data.total; |
| 86 | + document.getElementById('property-name').textContent = data.properties[0].name; |
| 87 | + }); |
| 88 | + </script> |
| 89 | + </body> |
| 90 | + </html> |
| 91 | + `); |
| 92 | + |
| 93 | + await page.waitForTimeout(1000); |
| 94 | + |
| 95 | + expect(await page.locator('#property-count').textContent()).toBe('1'); |
| 96 | + expect(await page.locator('#property-name').textContent()).toBe('Test Property'); |
| 97 | + }); |
| 98 | + |
| 99 | + test('should mock purchase transaction endpoint', async ({ page }) => { |
| 100 | + await page.route('**/api/properties/*/purchase', async (route) => { |
| 101 | + const postData = route.request().postDataJSON(); |
| 102 | + |
| 103 | + await route.fulfill({ |
| 104 | + status: 200, |
| 105 | + contentType: 'application/json', |
| 106 | + body: JSON.stringify({ |
| 107 | + success: true, |
| 108 | + transactionHash: '0xmocked123', |
| 109 | + amount: postData.amount, |
| 110 | + totalCost: postData.amount * 100, |
| 111 | + }), |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + await page.setContent(` |
| 116 | + <!DOCTYPE html> |
| 117 | + <html> |
| 118 | + <head><title>Purchase Test</title></head> |
| 119 | + <body> |
| 120 | + <div id="tx-hash"></div> |
| 121 | + <div id="cost"></div> |
| 122 | + <script> |
| 123 | + fetch('/api/properties/test-1/purchase', { |
| 124 | + method: 'POST', |
| 125 | + headers: { 'Content-Type': 'application/json' }, |
| 126 | + body: JSON.stringify({ amount: 10, walletAddress: '0x123' }) |
| 127 | + }) |
| 128 | + .then(res => res.json()) |
| 129 | + .then(data => { |
| 130 | + document.getElementById('tx-hash').textContent = data.transactionHash; |
| 131 | + document.getElementById('cost').textContent = data.totalCost; |
| 132 | + }); |
| 133 | + </script> |
| 134 | + </body> |
| 135 | + </html> |
| 136 | + `); |
| 137 | + |
| 138 | + await page.waitForTimeout(1000); |
| 139 | + |
| 140 | + expect(await page.locator('#tx-hash').textContent()).toBe('0xmocked123'); |
| 141 | + expect(await page.locator('#cost').textContent()).toBe('1000'); |
| 142 | + }); |
| 143 | +}); |
0 commit comments