Created
February 24, 2019 20:40
-
-
Save AaronLayton/00d15803fd0acedef1851bc679c65049 to your computer and use it in GitHub Desktop.
Help me please internetz
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
export const setPanelLoading = elm => { | |
elm.classList.add('is-loading'); | |
}; | |
export const setPanelLoaded = elm => { | |
elm.classList.remove('is-loading'); | |
}; | |
export const panelStatusUpdater = (container, promiseToAwait, timeout = 300) => { | |
const deferredOverlay = setTimeout(() => { | |
setPanelLoading(container); | |
}, timeout); | |
return promiseToAwait; | |
.finally(data => { | |
clearTimeout(deferredOverlay); | |
setPanelLoaded(container); | |
return data; | |
}); | |
}; |
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
import * as PSU from '../src/utils/panel-status-updater'; | |
//jest.useFakeTimers(); | |
describe('panel status updater', () => { | |
describe('setPanelLoading', () => { | |
it('should add `is-loading` class to target element', () => { | |
const target = document.createElement("div"); | |
PSU.setPanelLoading(target); | |
expect(target.classList.contains("is-loading")).toBeTruthy(); | |
}); | |
}); | |
describe('setPanelLoaded', () => { | |
it('should remove `is-loading` class to target element', () => { | |
const target = document.createElement("div"); | |
target.class = "is-loading"; | |
PSU.setPanelLoaded(target); | |
expect(target.classList.contains("is-loading")).toBeFalsy(); | |
}); | |
}); | |
describe('panelStatusUpdater', () => { | |
it('should call `setPanelLoading` internally after a timeout', (done) => { | |
PSU.setPanelLoading = jest.fn(); | |
const target = document.createElement("div"); | |
const testPromise = new Promise(resolve => { | |
setTimeout(() => { | |
resolve("data"); | |
}, 2); | |
}); | |
const results = PSU.panelStatusUpdater(target, testPromise, 1) | |
.then(data => { | |
console.log(PSU.setPanelLoading); | |
console.log(PSU.setPanelLoading.mock.calls.length); | |
expect(data).toBe("data"); | |
done(); | |
}) | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment