Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

Commit

Permalink
Fix getDatasFromCache with nested array
Browse files Browse the repository at this point in the history
Replace object parameter by simple parameter on createStep, render, afterRender
Update tests
Fix test on router constructor
Call step render without parameter if no datas is available in the cache
  • Loading branch information
yoriiis committed Apr 3, 2020
1 parent c58bb8e commit bff79c7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 44 deletions.
4 changes: 2 additions & 2 deletions dist/step-manager.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/dist/demo.js

Large diffs are not rendered by default.

32 changes: 12 additions & 20 deletions src/__tests__/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ describe('Router constructor', () => {
defaultRoute: 'people',
stepsOrder: ['people', 'planet'],
steps: {
people: new StepPeople(),
planet: new StepPlanet()
people: expect.any(Object),
planet: expect.any(Object)
},
getDatasFromCache: expect.any(Function)
});
expect(router.options.steps.people).toBeInstanceOf(StepPeople);
expect(router.options.steps.planet).toBeInstanceOf(StepPlanet);
expect(router.reverseNavigation).toBe(false);
expect(router.stepCreated).toBe(false);
expect(router.applicationReady).toBe(false);
Expand Down Expand Up @@ -205,9 +207,7 @@ describe('Router stepCanBeDisplayed', () => {
expect(router.getPreviousRoute).toHaveBeenCalledWith(event);
expect(router.previousRoute).toBe('people');
expect(router.destroyStep).toHaveBeenCalledWith('people');
expect(router.createStep).toHaveBeenCalledWith({
route: 'planet'
});
expect(router.createStep).toHaveBeenCalledWith('planet');
});

it('Should call the stepCanBeDisplayed function without event', () => {
Expand Down Expand Up @@ -239,9 +239,7 @@ describe('Router stepCanBeDisplayed', () => {
expect(router.getPreviousRoute).not.toHaveBeenCalled();
expect(router.previousRoute).toBe('planet');
expect(router.destroyStep).toHaveBeenCalledWith('planet');
expect(router.createStep).toHaveBeenCalledWith({
route: 'people'
});
expect(router.createStep).toHaveBeenCalledWith('people');
expect(router.stepRedirected.redirect).toBe(false);
});

Expand Down Expand Up @@ -360,12 +358,10 @@ describe('Router createStep', () => {
}));
router.options.steps[route].render = jest.fn();

router.createStep({ route });
router.createStep(route);

expect(router.options.getDatasFromCache).toHaveBeenCalledWith([route]);
expect(router.options.steps[route].render).toHaveBeenCalledWith({
datas: true
});
expect(router.options.steps[route].render).toHaveBeenCalledWith(true);
expect(router.applicationReady).toBe(true);
});

Expand All @@ -375,12 +371,10 @@ describe('Router createStep', () => {
router.options.getDatasFromCache = jest.fn().mockImplementation(() => null);
router.options.steps[route].render = jest.fn();

router.createStep({ route });
router.createStep(route);

expect(router.options.getDatasFromCache).toHaveBeenCalledWith([route]);
expect(router.options.steps[route].render).toHaveBeenCalledWith({
datas: null
});
expect(router.options.steps[route].render).toHaveBeenCalled();
expect(router.applicationReady).toBe(true);
});

Expand All @@ -391,12 +385,10 @@ describe('Router createStep', () => {
router.options.steps[route].render = jest.fn();

router.applicationReady = true;
router.createStep({ route });
router.createStep(route);

expect(router.options.getDatasFromCache).toHaveBeenCalledWith([route]);
expect(router.options.steps[route].render).toHaveBeenCalledWith({
datas: null
});
expect(router.options.steps[route].render).toHaveBeenCalled();
expect(router.applicationReady).toBe(true);
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/steps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ describe('Steps render', () => {
steps.afterRender = jest.fn();
steps.getTemplate = jest.fn().mockImplementation(() => 'CONTENT');

steps.render({ datas });
steps.render(datas);

expect(steps.requestOptions).toHaveBeenCalled();
expect(steps.getTemplate).toHaveBeenCalled();
expect(document.body.innerHTML).toBe(
'<div id="steps"><div class="step-people"><button data-step-previous=""></button><button data-step-next=""></button></div>CONTENT</div>'
);
expect(steps.afterRender).toHaveBeenCalledWith({ datas });
expect(steps.afterRender).toHaveBeenCalledWith(datas);
});
});

Expand All @@ -77,7 +77,7 @@ describe('Steps afterRender', () => {
steps.renderDatasFromCache = jest.fn();

document.querySelector('#steps').innerHTML = '<div class="step-people"></div>';
steps.afterRender({ datas });
steps.afterRender(datas);

expect(steps.currentStep).toBe(document.querySelector('.step-people'));
expect(steps.addEvents).toHaveBeenCalled();
Expand All @@ -88,7 +88,7 @@ describe('Steps afterRender', () => {
steps.addEvents = jest.fn();
steps.renderDatasFromCache = jest.fn();

steps.afterRender({ datas: null });
steps.afterRender(null);

expect(steps.renderDatasFromCache).not.toHaveBeenCalled();
});
Expand Down
2 changes: 1 addition & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Manager {
defaultRoute: results.defaultRoute,
stepsOrder: results.stepsOrder,
steps: this.steps,
getDatasFromCache: (...filters) => this.CacheManager.getDatasFromCache(filters)
getDatasFromCache: filters => this.CacheManager.getDatasFromCache(filters)
});

// Initialize the router
Expand Down
18 changes: 8 additions & 10 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,15 @@ export default class Router {
this.destroyStep(this.previousRoute);

// Create the new step on destruction callback
this.createStep({
route: this.currentRoute
});
this.createStep(this.currentRoute);

this.stepCreated = true;
}
}

// If destroy method was not called, create the step now
if (!this.stepCreated) {
this.createStep({
route: this.currentRoute
});
this.createStep(this.currentRoute);
}

// Reset the redirect marker
Expand Down Expand Up @@ -177,14 +173,16 @@ export default class Router {
*
* @param {String} route Route of the step
*/
createStep ({ route }) {
createStep (route) {
// Get datas from cache before render the step
const stepDatas = this.options.getDatasFromCache([route]);

// Call the render method of the step
this.options.steps[route].render({
datas: stepDatas ? stepDatas[route].datas : null
});
if (stepDatas) {
this.options.steps[route].render(stepDatas[route].datas);
} else {
this.options.steps[route].render();
}

// Prevent step created before application ready
if (!this.applicationReady) {
Expand Down
8 changes: 3 additions & 5 deletions src/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@ export default class Steps {
*
* @param {Object} datas Datas from the cache
*/
render ({ datas }) {
render (datas) {
this.options = this.requestOptions();

// Insert the generated HTML for the step
// Get the template from the specific class and send it datas
this.options.element.insertAdjacentHTML('beforeend', this.getTemplate());

// The DOM is up to date, trigger the after render method with datas from the cache
this.afterRender({
datas: datas
});
this.afterRender(datas);
}

/**
* Function executed after the render of the step
*
* @param {Object} datas Datas from the cache
*/
afterRender ({ datas } = {}) {
afterRender (datas) {
// Set cached selector
this.currentStep = this.options.element.querySelector(this.selector);

Expand Down

0 comments on commit bff79c7

Please sign in to comment.