2024-03-14 13:50:57 +00:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { SurveyResponseService } from '../services/surveyResponse.service';
|
|
|
|
import { MongoRepository } from 'typeorm';
|
|
|
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
|
|
import { SurveyResponse } from 'src/models/surveyResponse.entity';
|
|
|
|
|
|
|
|
describe('SurveyResponseService', () => {
|
|
|
|
let service: SurveyResponseService;
|
|
|
|
let surveyResponseRepository: MongoRepository<SurveyResponse>;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
|
|
providers: [
|
|
|
|
SurveyResponseService,
|
|
|
|
{
|
|
|
|
provide: getRepositoryToken(SurveyResponse),
|
|
|
|
useValue: {
|
|
|
|
create: jest.fn(),
|
|
|
|
save: jest.fn(),
|
|
|
|
count: jest.fn(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}).compile();
|
|
|
|
|
|
|
|
service = module.get<SurveyResponseService>(SurveyResponseService);
|
|
|
|
surveyResponseRepository = module.get<MongoRepository<SurveyResponse>>(
|
|
|
|
getRepositoryToken(SurveyResponse),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a survey response', async () => {
|
|
|
|
const surveyData = {
|
|
|
|
data: {},
|
|
|
|
clientTime: new Date(),
|
2024-08-06 09:30:12 +00:00
|
|
|
diffTime: 0,
|
2024-03-14 13:50:57 +00:00
|
|
|
surveyId: 'testId',
|
|
|
|
surveyPath: 'testPath',
|
|
|
|
optionTextAndId: {},
|
|
|
|
};
|
|
|
|
jest
|
|
|
|
.spyOn(surveyResponseRepository, 'create')
|
|
|
|
.mockImplementation((data) => {
|
|
|
|
const surveyResponse = new SurveyResponse();
|
|
|
|
for (const key in data) {
|
|
|
|
surveyResponse[key] = data[key];
|
|
|
|
}
|
|
|
|
return surveyResponse;
|
|
|
|
});
|
|
|
|
jest
|
|
|
|
.spyOn(surveyResponseRepository, 'save')
|
|
|
|
.mockImplementation((surveyResponse: SurveyResponse) => {
|
|
|
|
return Promise.resolve(surveyResponse);
|
|
|
|
});
|
|
|
|
|
|
|
|
await service.createSurveyResponse(surveyData);
|
|
|
|
|
|
|
|
expect(surveyResponseRepository.create).toHaveBeenCalledWith({
|
|
|
|
surveyPath: surveyData.surveyPath,
|
|
|
|
data: surveyData.data,
|
|
|
|
clientTime: surveyData.clientTime,
|
2024-08-06 09:30:12 +00:00
|
|
|
diffTime: surveyData.diffTime,
|
2024-03-14 13:50:57 +00:00
|
|
|
pageId: surveyData.surveyId,
|
|
|
|
secretKeys: [],
|
|
|
|
optionTextAndId: surveyData.optionTextAndId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get the total survey response count by path', async () => {
|
|
|
|
const surveyPath = 'testPath';
|
|
|
|
const count = 10;
|
|
|
|
jest.spyOn(surveyResponseRepository, 'count').mockResolvedValue(count);
|
|
|
|
|
|
|
|
const result = await service.getSurveyResponseTotalByPath(surveyPath);
|
|
|
|
|
|
|
|
expect(result).toEqual(count);
|
|
|
|
expect(surveyResponseRepository.count).toHaveBeenCalledWith({
|
|
|
|
where: {
|
|
|
|
surveyPath,
|
|
|
|
'curStatus.status': { $ne: 'removed' },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|