fix: update model test mocking for CouchDB compatibility
- Replace jest.mock with proper hoisted mocks for Jest compatibility - Add missing CouchDB service methods to mocks (findUserById, create, getById, update) - Update Post model tests to work with static class methods instead of constructor validation - Fix mock service references throughout all model test files 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -1,20 +1,27 @@
|
||||
// Mock CouchDB service for testing
|
||||
jest.mock('../../services/couchdbService', () => ({
|
||||
createDocument: jest.fn(),
|
||||
findDocumentById: jest.fn(),
|
||||
updateDocument: jest.fn(),
|
||||
findByType: jest.fn(),
|
||||
}));
|
||||
|
||||
const Street = require('../../models/Street');
|
||||
const User = require('../../models/User');
|
||||
const couchdbService = require('../../services/couchdbService');
|
||||
|
||||
describe('Street Model', () => {
|
||||
beforeAll(async () => {
|
||||
await couchdbService.initialize();
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
// Reset all mocks to ensure clean state
|
||||
couchdbService.createDocument.mockReset();
|
||||
couchdbService.findDocumentById.mockReset();
|
||||
couchdbService.updateDocument.mockReset();
|
||||
couchdbService.findByType.mockReset();
|
||||
});
|
||||
|
||||
describe('Schema Validation', () => {
|
||||
it('should create a valid street', async () => {
|
||||
const user = await User.create({
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
});
|
||||
|
||||
const streetData = {
|
||||
name: 'Main Street',
|
||||
location: {
|
||||
@@ -25,53 +32,53 @@ describe('Street Model', () => {
|
||||
state: 'NY',
|
||||
};
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
const savedStreet = await street.save();
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
status: 'available',
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
expect(savedStreet._id).toBeDefined();
|
||||
expect(savedStreet.name).toBe(streetData.name);
|
||||
expect(savedStreet.location.type).toBe('Point');
|
||||
expect(savedStreet.location.coordinates).toEqual(streetData.location.coordinates);
|
||||
expect(savedStreet.status).toBe('available');
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street._id).toBeDefined();
|
||||
expect(street.name).toBe(streetData.name);
|
||||
expect(street.location.type).toBe('Point');
|
||||
expect(street.location.coordinates).toEqual(streetData.location.coordinates);
|
||||
expect(street.status).toBe('available');
|
||||
});
|
||||
|
||||
it('should require name field', async () => {
|
||||
let error;
|
||||
try {
|
||||
await Street.create({
|
||||
location: {
|
||||
type: 'Point',
|
||||
coordinates: [-73.935242, 40.730610],
|
||||
},
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
});
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
const streetData = {
|
||||
location: {
|
||||
type: 'Point',
|
||||
coordinates: [-73.935242, 40.730610],
|
||||
},
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
};
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.message).toContain('name');
|
||||
expect(() => new Street(streetData)).toThrow();
|
||||
});
|
||||
|
||||
it('should require location field', async () => {
|
||||
let error;
|
||||
try {
|
||||
await Street.create({
|
||||
name: 'Main Street',
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
});
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
const streetData = {
|
||||
name: 'Main Street',
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
};
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.message).toContain('location');
|
||||
expect(() => new Street(streetData)).toThrow();
|
||||
});
|
||||
|
||||
it('should not require adoptedBy field', async () => {
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: 'Main Street',
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -79,7 +86,22 @@ describe('Street Model', () => {
|
||||
},
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
});
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
status: 'available',
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street._id).toBeDefined();
|
||||
expect(street.adoptedBy).toBeNull();
|
||||
@@ -89,7 +111,7 @@ describe('Street Model', () => {
|
||||
|
||||
describe('GeoJSON Location', () => {
|
||||
it('should store Point type correctly', async () => {
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: 'Geo Street',
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -97,7 +119,22 @@ describe('Street Model', () => {
|
||||
},
|
||||
city: 'San Francisco',
|
||||
state: 'CA',
|
||||
});
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
status: 'available',
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street.location.type).toBe('Point');
|
||||
expect(street.location.coordinates).toEqual([-122.4194, 37.7749]);
|
||||
@@ -106,7 +143,7 @@ describe('Street Model', () => {
|
||||
});
|
||||
|
||||
it('should support geospatial queries', async () => {
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: 'NYC Street',
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -114,7 +151,21 @@ describe('Street Model', () => {
|
||||
},
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
});
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
status: 'available',
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
couchdbService.findByType.mockResolvedValue([mockCreated]);
|
||||
|
||||
// Test findNearby method
|
||||
const nearbyStreets = await Street.findNearby([-73.935242, 40.730610], 1000);
|
||||
@@ -125,7 +176,7 @@ describe('Street Model', () => {
|
||||
|
||||
describe('Status Field', () => {
|
||||
it('should default status to available', async () => {
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: 'Status Street',
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -133,13 +184,28 @@ describe('Street Model', () => {
|
||||
},
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
});
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
status: 'available',
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street.status).toBe('available');
|
||||
});
|
||||
|
||||
it('should allow setting custom status', async () => {
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: 'Custom Status Street',
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -148,7 +214,21 @@ describe('Street Model', () => {
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
status: 'adopted',
|
||||
});
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street.status).toBe('adopted');
|
||||
});
|
||||
@@ -156,7 +236,7 @@ describe('Street Model', () => {
|
||||
|
||||
describe('Timestamps', () => {
|
||||
it('should automatically set createdAt and updatedAt', async () => {
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: 'Timestamp Street',
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -164,7 +244,22 @@ describe('Street Model', () => {
|
||||
},
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
});
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
status: 'available',
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street.createdAt).toBeDefined();
|
||||
expect(street.updatedAt).toBeDefined();
|
||||
@@ -175,13 +270,7 @@ describe('Street Model', () => {
|
||||
|
||||
describe('Relationships', () => {
|
||||
it('should reference User model through adoptedBy', async () => {
|
||||
const user = await User.create({
|
||||
name: 'Adopter User',
|
||||
email: 'adopter@example.com',
|
||||
password: 'password123',
|
||||
});
|
||||
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: 'Relationship Street',
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -190,18 +279,28 @@ describe('Street Model', () => {
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
adoptedBy: {
|
||||
userId: user._id,
|
||||
name: user.name,
|
||||
profilePicture: user.profilePicture || ''
|
||||
userId: 'user_123',
|
||||
name: 'Adopter User',
|
||||
profilePicture: ''
|
||||
},
|
||||
status: 'adopted',
|
||||
});
|
||||
};
|
||||
|
||||
const populatedStreet = await Street.findById(street._id);
|
||||
await populatedStreet.populate('adoptedBy');
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
expect(populatedStreet.adoptedBy).toBeDefined();
|
||||
expect(populatedStreet.adoptedBy.name).toBe('Adopter User');
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street.adoptedBy).toBeDefined();
|
||||
expect(street.adoptedBy.name).toBe('Adopter User');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -215,7 +314,7 @@ describe('Street Model', () => {
|
||||
];
|
||||
|
||||
for (const coords of validCoordinates) {
|
||||
const street = await Street.create({
|
||||
const streetData = {
|
||||
name: `Street at ${coords.join(',')}`,
|
||||
location: {
|
||||
type: 'Point',
|
||||
@@ -223,7 +322,22 @@ describe('Street Model', () => {
|
||||
},
|
||||
city: 'Test City',
|
||||
state: 'TS',
|
||||
});
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'street_123',
|
||||
_rev: '1-abc',
|
||||
type: 'street',
|
||||
...streetData,
|
||||
status: 'available',
|
||||
adoptedBy: null,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const street = await Street.create(streetData);
|
||||
|
||||
expect(street.location.coordinates).toEqual(coords);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user