-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (strf-8740) cover StencilStart.assembleTemplates() with tests
- Loading branch information
MaxGenash
committed
Oct 13, 2020
1 parent
f2e2724
commit 0adf1f8
Showing
2 changed files
with
148 additions
and
35 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,123 @@ | ||
const fetchMockModule = require('fetch-mock-jest'); | ||
const path = require('path'); | ||
|
||
const StencilStart = require('./stencil-start'); | ||
|
||
afterAll(() => jest.restoreAllMocks()); | ||
|
||
describe('StencilStart unit tests', () => { | ||
const getBrowserSyncStub = () => ({ | ||
watch: jest.fn(), | ||
init: jest.fn(), | ||
}); | ||
const getFetchStub = () => fetchMockModule.sandbox(); | ||
const getFsStub = () => ({ | ||
existsSync: jest.fn(), | ||
}); | ||
const getFsUtilsStub = () => ({ | ||
parseJsonFile: jest.fn(), | ||
recursiveReadDir: jest.fn(), | ||
}); | ||
const getCliCommonStub = () => ({ | ||
checkNodeVersion: jest.fn(), | ||
}); | ||
const getThemeConfigManagerStub = () => ({}); | ||
const getBuildConfigMangerStub = () => ({}); | ||
const getTemplateAssemblerStub = () => ({}); | ||
const getCyclesDetectorConstructorStub = () => jest.fn(); | ||
const getLoggerStub = () => ({ | ||
log: jest.fn(), | ||
error: jest.fn(), | ||
}); | ||
|
||
const createStencilStartInstance = ({ | ||
browserSync, | ||
fetch, | ||
fs, | ||
fsUtils, | ||
cliCommon, | ||
themeConfigManager, | ||
buildConfigManger, | ||
templateAssembler, | ||
CyclesDetector, | ||
logger, | ||
} = {}) => { | ||
const passedArgs = { | ||
browserSync: browserSync || getBrowserSyncStub(), | ||
fetch: fetch || getFetchStub(), | ||
fs: fs || getFsStub(), | ||
fsUtils: fsUtils || getFsUtilsStub(), | ||
cliCommon: cliCommon || getCliCommonStub(), | ||
themeConfigManager: themeConfigManager || getThemeConfigManagerStub(), | ||
buildConfigManger: buildConfigManger || getBuildConfigMangerStub(), | ||
templateAssembler: templateAssembler || getTemplateAssemblerStub(), | ||
CyclesDetector: CyclesDetector || getCyclesDetectorConstructorStub(), | ||
logger: logger || getLoggerStub(), | ||
}; | ||
const instance = new StencilStart(passedArgs); | ||
|
||
return { | ||
passedArgs, | ||
instance, | ||
}; | ||
}; | ||
|
||
describe('constructor', () => { | ||
it('should create an instance of StencilStart without options parameters passed', async () => { | ||
const instance = new StencilStart(); | ||
|
||
expect(instance).toBeInstanceOf(StencilStart); | ||
}); | ||
|
||
it('should create an instance of StencilStart with all options parameters passed', async () => { | ||
const { instance } = createStencilStartInstance(); | ||
|
||
expect(instance).toBeInstanceOf(StencilStart); | ||
}); | ||
}); | ||
|
||
describe('assembleTemplates method', () => { | ||
it('should obtain names of all templates in the passed templatesPath and return results of call templateAssembler.assemble for each template name', async () => { | ||
const templatesPath = '/some/absolute/templates/path'; | ||
const templateNamesMock = ['layout/base', 'pages/page1']; | ||
const filesPathsMock = [ | ||
templatesPath + path.sep + templateNamesMock[0] + '.html', | ||
templatesPath + path.sep + templateNamesMock[1] + '.html', | ||
]; | ||
const templateAssemblerResults = [ | ||
{ | ||
[templateNamesMock[0]]: 'html file content 1', | ||
}, | ||
{ | ||
[templateNamesMock[0]]: 'html file content 1', | ||
[templateNamesMock[1]]: 'html file content 2', | ||
}, | ||
]; | ||
const fsUtilsStub = { | ||
recursiveReadDir: jest.fn().mockResolvedValue(filesPathsMock), | ||
}; | ||
const templateAssemblerStub = { | ||
assemble: jest | ||
.fn() | ||
.mockImplementationOnce((p, n, cb) => cb(null, templateAssemblerResults[0])) | ||
.mockImplementationOnce((p, n, cb) => cb(null, templateAssemblerResults[1])), | ||
}; | ||
|
||
const { instance } = createStencilStartInstance({ | ||
fsUtils: fsUtilsStub, | ||
templateAssembler: templateAssemblerStub, | ||
}); | ||
const result = await instance.assembleTemplates(templatesPath); | ||
|
||
expect(fsUtilsStub.recursiveReadDir).toHaveBeenCalledTimes(1); | ||
expect(fsUtilsStub.recursiveReadDir).toHaveBeenCalledWith(templatesPath, ['!*.html']); | ||
|
||
expect(templateAssemblerStub.assemble.mock.calls).toEqual([ | ||
[templatesPath, templateNamesMock[0], expect.any(Function)], | ||
[templatesPath, templateNamesMock[1], expect.any(Function)], | ||
]); | ||
|
||
expect(result).toStrictEqual(templateAssemblerResults); | ||
}); | ||
}); | ||
}); |