Conversation
✅ Deploy Preview for inquisitive-praline-20c1ca ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for maptyper ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Co-authored-by: yuletide <58695+yuletide@users.noreply.github.com>
Co-authored-by: yuletide <58695+yuletide@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive test coverage for the MapTyper class, which previously had no tests. The test suite includes 19 tests covering constructor validation, async font initialization, text-to-GeoJSON conversion, SVG generation, and bounds calculation. The PR also establishes the test infrastructure using Jest with Babel transpilation for ES modules.
Changes:
- Added comprehensive test suite for MapTyper class with 19 tests covering core functionality
- Set up Jest testing infrastructure with Babel transpilation for ES module support
- Added test scripts (test, test:watch, test:coverage) and configured coverage collection
- Updated .gitignore to exclude node_modules and coverage directories
Reviewed changes
Copilot reviewed 3 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/maptyper.test.js | New test file with 19 tests covering MapTyper constructor, font initialization, text conversion, SVG generation, bounds calculation, and options configuration |
| package.json | Added Jest 30.x, Babel, and related test dependencies; configured test scripts |
| jest.config.js | New Jest configuration with jsdom environment, Babel transform, and coverage settings |
| babel.config.js | New Babel configuration for ES module transpilation in tests |
| .gitignore | Added node_modules and coverage to ignored files |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith(typer); | ||
| }); |
There was a problem hiding this comment.
The tests do not cover error handling when font loading fails. The MapTyper.initFont method has error handling that rejects the promise with an error message when TextToSVG.load fails (line 56 in maptyper.js: reject('Error loading font ' + err)), but there are no tests that verify this behavior. Add a test case that mocks TextToSVG.load to call the callback with an error and verify that the promise is properly rejected.
| }); | |
| }); | |
| test('should reject when font loading fails', async () => { | |
| // Access the mocked text-to-svg module | |
| const TextToSVG = require('text-to-svg').default; | |
| const loadError = new Error('load failed'); | |
| // For this test, make load call its callback with an error | |
| TextToSVG.load.mockImplementationOnce((fontPath, callback) => { | |
| callback(loadError); | |
| }); | |
| await expect(MapTyper.initFont('/bad/font.ttf')).rejects.toMatch( | |
| 'Error loading font' | |
| ); | |
| }); |
| describe('textToFeatures', () => { | ||
| test('should convert text to GeoJSON features after font loads', async () => { | ||
| const typer = new MapTyper('/path/to/font.ttf'); | ||
|
|
||
| // Wait for font to load | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| const result = typer.textToFeatures('Hello'); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.type).toBe('FeatureCollection'); | ||
| expect(result.features).toBeInstanceOf(Array); | ||
| }); | ||
|
|
||
| test('should handle empty text', async () => { | ||
| const typer = new MapTyper('/path/to/font.ttf'); | ||
|
|
||
| // Wait for font to load | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| const result = typer.textToFeatures(''); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.type).toBe('FeatureCollection'); | ||
| }); | ||
|
|
||
| test('should handle text with whitespace', async () => { | ||
| const typer = new MapTyper('/path/to/font.ttf'); | ||
|
|
||
| // Wait for font to load | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| const result = typer.textToFeatures('Hello World'); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.type).toBe('FeatureCollection'); | ||
| }); | ||
|
|
||
| test('should handle special characters', async () => { | ||
| const typer = new MapTyper('/path/to/font.ttf'); | ||
|
|
||
| // Wait for font to load | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| const result = typer.textToFeatures('Hello! @#$%'); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.type).toBe('FeatureCollection'); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
The tests do not cover the scenario where textToFeatures is called before the font is loaded. The MapTyper.textToFeatures method has special handling for this case (lines 102-106 in maptyper.js), logging an error and attempting to return a promise. However, there are no tests that verify this behavior. Add a test that calls textToFeatures immediately after construction (before the font loads) to ensure this edge case is properly handled.
No test coverage existed for the MapTyper class. Added comprehensive unit tests.
Changes
Test suite: 19 tests covering MapTyper functionality (91% statement coverage)
Test infrastructure: Jest with Babel transpilation for ES modules
Scripts:
npm test,npm run test:watch,npm run test:coverageExample
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.