-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for lookup functions in BasePackageLoader.
- Loading branch information
1 parent
a3053b1
commit fcda419
Showing
2 changed files
with
236 additions
and
84 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 |
---|---|---|
@@ -1,36 +1,55 @@ | ||
import { loggerSpy } from '../testhelpers'; | ||
import { mock } from 'jest-mock-extended'; | ||
import { BasePackageLoader, BasePackageLoaderOptions } from '../../src/loader/BasePackageLoader'; | ||
import { LoadStatus } from '../../src/loader/PackageLoader'; | ||
import { PackageDB } from '../../src/db'; | ||
import { PackageCache } from '../../src/cache'; | ||
import { RegistryClient } from '../../src/registry'; | ||
import { CurrentBuildClient } from '../../src/current'; | ||
import { jest } from '@jest/globals'; | ||
import { defaultPackageLoader, defaultPackageLoaderWithLocalResources } from '../../src/loader'; | ||
import { BasePackageLoader } from '../../src/loader/BasePackageLoader'; | ||
import { DiskBasedPackageCache } from '../../src/cache/DiskBasedPackageCache'; | ||
|
||
jest.mock('sql.js', () => { | ||
return () => { | ||
class Database {} | ||
return { | ||
Database | ||
}; | ||
}; | ||
}); | ||
jest.mock('../../src/db/SQLJSPackageDB'); | ||
jest.mock('../../src/cache/DiskBasedPackageCache', () => { | ||
return { | ||
DiskBasedPackageCache: jest.fn().mockImplementation(() => { | ||
return {}; | ||
}) | ||
}; | ||
}); | ||
jest.mock('../../src/registry/DefaultRegistryClient'); | ||
jest.mock('../../src/current/BuildDotFhirDotOrgClient'); | ||
|
||
describe('DefaultPackageLoader', () => { | ||
async function makeMyDefaultPackageLoader( | ||
options: BasePackageLoaderOptions | ||
): Promise<BasePackageLoader> { | ||
const packageDBMock = mock<PackageDB>(); | ||
const packageCacheMock = mock<PackageCache>(); | ||
const registryClientMock = mock<RegistryClient>(); | ||
const currentBuildClientMock = mock<CurrentBuildClient>(); | ||
const loader = new BasePackageLoader( | ||
packageDBMock, | ||
packageCacheMock, | ||
registryClientMock, | ||
currentBuildClientMock, | ||
{ log: options.log } | ||
); | ||
return loader; | ||
} | ||
beforeEach(() => { | ||
(DiskBasedPackageCache as jest.Mock).mockClear(); | ||
}); | ||
|
||
it('should create a package loader with mock default package loader', async () => { | ||
const loader = await makeMyDefaultPackageLoader({ log: loggerSpy.log }); | ||
loader.getPackageLoadStatus = jest.fn().mockReturnValueOnce(LoadStatus.LOADED); | ||
it('should create an instance of BasePackageLoader with no local resource folders', async () => { | ||
const loader = await defaultPackageLoader({ log: loggerSpy.log }); | ||
expect(loader).toBeInstanceOf(BasePackageLoader); | ||
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledTimes(1); | ||
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledWith(expect.any(String), [], { | ||
log: loggerSpy.log | ||
}); | ||
}); | ||
|
||
const result = await loader.loadPackage('some.ig', '1.2.3'); | ||
expect(result).toBe(LoadStatus.LOADED); | ||
expect(loader.getPackageLoadStatus).toHaveBeenCalledWith('some.ig', '1.2.3'); | ||
it('should create an instance of BasePackageLoader with specified local resource folders', async () => { | ||
const loader = await defaultPackageLoaderWithLocalResources( | ||
['/some/folder', '/another/good/folder'], | ||
{ log: loggerSpy.log } | ||
); | ||
expect(loader).toBeInstanceOf(BasePackageLoader); | ||
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledTimes(1); | ||
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledWith( | ||
expect.any(String), | ||
['/some/folder', '/another/good/folder'], | ||
{ | ||
log: loggerSpy.log | ||
} | ||
); | ||
}); | ||
}); |