-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start migration updateProjectSource saga * Update updateProjectSource * Use redux-logic for errors * Remove errors sagas unit tests * Reorganize errors logic * Start validateProjectOnChange tests * Finish validateProjectOnChange specs * Write test for validateSource helper * Start validateCurrentProject test * Finish validateProjectOnChange tests * Refactor validateSource logic tests * Lint validateCurrentProject and validateProjectOnChange * Improve validateCurrentProject and validateProjectOnChange tests * Ensure that only current validations are dispatched * Lint errors logic * Start validateProject logic * Refactor errors logic to use validateProject * Run lint * Refactor validateProject Co-authored-by: Mat Brown <[email protected]>
- Loading branch information
1 parent
4674d7d
commit 3314044
Showing
8 changed files
with
173 additions
and
233 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
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,78 @@ | ||
import { | ||
projectRestoredFromLastSession as projectRestoredFromLastSessionAction, | ||
snapshotImported as snapshotImportedAction, | ||
} from '../../actions/clients'; | ||
import {validatedSource} from '../../actions/errors'; | ||
import { | ||
changeCurrentProject as changeCurrentProjectAction, | ||
gistImported as gistImportedAction, | ||
toggleLibrary as toggleLibraryAction, | ||
updateProjectSource as updateProjectSourceAction, | ||
} from '../../actions/projects'; | ||
import validateProject from '../validateProject'; | ||
|
||
import {applyActions, makeTestLogic} from './helpers'; | ||
|
||
import {firebaseProjectFactory} from '@factories/data/firebase'; | ||
import {consoleErrorFactory} from '@factories/validations/errors'; | ||
|
||
jest.mock('../../analyzers'); | ||
|
||
const mockCssValidationErrors = [ | ||
consoleErrorFactory.build({ | ||
text: 'You have a starting { but no ending } to go with it.', | ||
}), | ||
]; | ||
|
||
const mockHtmlValidationErrors = [ | ||
consoleErrorFactory.build({ | ||
text: 'Closing tag missing', | ||
}), | ||
]; | ||
|
||
jest.mock('../../validations', () => ({ | ||
css: jest.fn(() => mockCssValidationErrors), | ||
html: jest.fn(() => mockHtmlValidationErrors), | ||
javascript: jest.fn(() => []), | ||
})); | ||
|
||
const testLogic = makeTestLogic(validateProject); | ||
for (const action of [ | ||
changeCurrentProjectAction, | ||
gistImportedAction, | ||
snapshotImportedAction, | ||
projectRestoredFromLastSessionAction, | ||
toggleLibraryAction, | ||
]) { | ||
test(`validates current project on ${action}`, async () => { | ||
const mockProject = firebaseProjectFactory.build(); | ||
const state = applyActions( | ||
projectRestoredFromLastSessionAction(mockProject), | ||
); | ||
|
||
const dispatch = await testLogic(action(mockProject.projectKey), { | ||
state, | ||
}); | ||
|
||
expect(dispatch).toHaveBeenCalledWith( | ||
validatedSource('html', mockHtmlValidationErrors), | ||
); | ||
expect(dispatch).toHaveBeenCalledWith( | ||
validatedSource('css', mockCssValidationErrors), | ||
); | ||
}); | ||
} | ||
|
||
test('UPDATE_PROJECT_SOURCE should validate newSource', async () => { | ||
const mockProject = firebaseProjectFactory.build(); | ||
const state = applyActions(projectRestoredFromLastSessionAction(mockProject)); | ||
|
||
const dispatch = await testLogic( | ||
updateProjectSourceAction(mockProject.projectKey, 'css', 'div {'), | ||
{state}, | ||
); | ||
|
||
expect(dispatch).toHaveBeenCalledWith( | ||
validatedSource('css', mockCssValidationErrors), | ||
); | ||
}); |
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,79 @@ | ||
import map from 'lodash-es/map'; | ||
import {createLogic} from 'redux-logic'; | ||
|
||
import {validatedSource} from '../actions/errors'; | ||
import Analyzer from '../analyzers'; | ||
import {getCurrentProject} from '../selectors'; | ||
import retryingFailedImports from '../util/retryingFailedImports'; | ||
|
||
function importValidations() { | ||
return retryingFailedImports(() => | ||
import( | ||
/* webpackChunkName: "mainAsync" */ | ||
'../validations' | ||
), | ||
); | ||
} | ||
|
||
async function validateSource({language, source, projectAttributes}, dispatch) { | ||
const validations = await importValidations(); | ||
const validate = validations[language]; | ||
const validationErrors = await validate(source, projectAttributes); | ||
dispatch(validatedSource(language, validationErrors)); | ||
} | ||
|
||
async function validateSources({sources, projectAttributes}, dispatch) { | ||
const validatePromises = map(Reflect.ownKeys(sources), language => | ||
validateSource( | ||
{ | ||
language, | ||
source: sources[language], | ||
projectAttributes, | ||
}, | ||
dispatch, | ||
), | ||
); | ||
|
||
await Promise.all(validatePromises); | ||
} | ||
|
||
export default createLogic({ | ||
type: [ | ||
'CHANGE_CURRENT_PROJECT', | ||
'GIST_IMPORTED', | ||
'SNAPSHOT_IMPORTED', | ||
'PROJECT_RESTORED_FROM_LAST_SESSION', | ||
'TOGGLE_LIBRARY', | ||
'UPDATE_PROJECT_SOURCE', | ||
], | ||
latest: true, | ||
async process( | ||
{ | ||
getState, | ||
action: { | ||
type, | ||
payload: {language, newValue}, | ||
}, | ||
}, | ||
dispatch, | ||
done, | ||
) { | ||
const state = getState(); | ||
const currentProject = getCurrentProject(state); | ||
const projectAttributes = new Analyzer(currentProject); | ||
|
||
if (type === 'UPDATE_PROJECT_SOURCE') { | ||
await validateSource( | ||
{language, source: newValue, projectAttributes}, | ||
dispatch, | ||
); | ||
} else { | ||
await validateSources( | ||
{sources: currentProject.sources, projectAttributes}, | ||
dispatch, | ||
); | ||
} | ||
|
||
done(); | ||
}, | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.