forked from doubtfire-lms/doubtfire-web
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added numbas service and numbas service test daniel
- Loading branch information
Showing
2 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { TestBed, fakeAsync, tick } from '@angular/core/testing'; | ||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
import { NumbasService } from '../numbas.service'; | ||
import { HttpRequest } from '@angular/common/http'; | ||
|
||
describe('NumbasService', () => { | ||
let numbasService: NumbasService; | ||
let httpMock: HttpTestingController; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [NumbasService], | ||
}); | ||
|
||
numbasService = TestBed.inject(NumbasService); | ||
httpMock = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
afterEach(() => { | ||
httpMock.verify(); | ||
}); | ||
|
||
it('should fetch resource as expected', fakeAsync(() => { | ||
const dummyBlob = new Blob(['dummy blob'], { type: 'text/html' }); | ||
|
||
const unitId = 'sampleUnitId'; | ||
const taskId = 'sampleTaskId'; | ||
const resourcePath = 'sampleResource.html'; | ||
|
||
numbasService.fetchResource(unitId, taskId, resourcePath).subscribe((blob) => { | ||
expect(blob.size).toBe(dummyBlob.size); | ||
expect(blob.type).toBe(dummyBlob.type); | ||
}); | ||
|
||
const req = httpMock.expectOne(`http://localhost:3000/api/numbas_api/${unitId}/${taskId}/${resourcePath}`); | ||
expect(req.request.method).toBe('GET'); | ||
|
||
req.flush(dummyBlob); | ||
|
||
tick(); | ||
})); | ||
|
||
it('should upload test as expected', fakeAsync(() => { | ||
const dummyResponse = { success: true, message: 'File uploaded successfully' }; | ||
|
||
const unitId = 'sampleUnitId'; | ||
const taskId = 'sampleTaskId'; | ||
const file = new File(['dummy content'], 'sample.txt', { type: 'text/plain' }); | ||
|
||
numbasService.uploadTest(unitId, taskId, file).subscribe((response) => { | ||
expect(response).toEqual(dummyResponse); | ||
}); | ||
|
||
const req = httpMock.expectOne(`http://localhost:3000/api/numbas_api/uploadNumbasTest`); | ||
expect(req.request.method).toBe('POST'); | ||
|
||
req.flush(dummyResponse); | ||
|
||
tick(); | ||
})); | ||
|
||
}); | ||
|
||
|