Commit a421fc26 authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Create and destroy fake clock on the same `it` function

There seems to be a pattern in our code that if a fake clock is used on
a single `it` function, the clock should be created and restored within
that function. I think it is beneficial from a readability perspective.
On the other hand, if the clock is used in several test functions it
should be declared on a parent context and restored on an `afterEach`
function.

I simplified an unrelated test using async.
parent 7c2ddbb7
...@@ -9,7 +9,6 @@ import { checkAccessibility } from '../../../test-util/accessibility'; ...@@ -9,7 +9,6 @@ import { checkAccessibility } from '../../../test-util/accessibility';
describe('Menu', () => { describe('Menu', () => {
let container; let container;
let clock;
const TestLabel = () => 'Test label'; const TestLabel = () => 'Test label';
const TestMenuItem = () => 'Test item'; const TestMenuItem = () => 'Test item';
...@@ -39,10 +38,6 @@ describe('Menu', () => { ...@@ -39,10 +38,6 @@ describe('Menu', () => {
afterEach(() => { afterEach(() => {
$imports.$restore(); $imports.$restore();
container.remove(); container.remove();
if (clock) {
clock.restore();
clock = null;
}
}); });
it('opens and closes when the toggle button is clicked', () => { it('opens and closes when the toggle button is clicked', () => {
...@@ -182,7 +177,7 @@ describe('Menu', () => { ...@@ -182,7 +177,7 @@ describe('Menu', () => {
it(`${ it(`${
shouldClose ? 'closes' : "doesn't close" shouldClose ? 'closes' : "doesn't close"
} when user performs a "${eventType}" (key: "${key}") on menu content`, () => { } when user performs a "${eventType}" (key: "${key}") on menu content`, () => {
clock = sinon.useFakeTimers(); const clock = sinon.useFakeTimers();
try { try {
const wrapper = createMenu({ defaultOpen: true }); const wrapper = createMenu({ defaultOpen: true });
wrapper.find('.Menu__content').simulate(eventType, { key }); wrapper.find('.Menu__content').simulate(eventType, { key });
......
...@@ -109,8 +109,6 @@ describe('SessionService', () => { ...@@ -109,8 +109,6 @@ describe('SessionService', () => {
}); });
context('when using a first party account', () => { context('when using a first party account', () => {
let clock;
beforeEach(() => { beforeEach(() => {
fakeApi.profile.read.returns( fakeApi.profile.read.returns(
Promise.resolve({ Promise.resolve({
...@@ -119,12 +117,6 @@ describe('SessionService', () => { ...@@ -119,12 +117,6 @@ describe('SessionService', () => {
); );
}); });
afterEach(() => {
if (clock) {
clock.restore();
}
});
it('should fetch profile data from the API', () => { it('should fetch profile data from the API', () => {
const session = createService(); const session = createService();
return session.load().then(() => { return session.load().then(() => {
...@@ -170,32 +162,28 @@ describe('SessionService', () => { ...@@ -170,32 +162,28 @@ describe('SessionService', () => {
}); });
}); });
it('should cache the returned profile data', () => { it('should cache the returned profile data', async () => {
const session = createService(); const session = createService();
return session await session.load();
.load() await session.load();
.then(() => {
return session.load();
})
.then(() => {
assert.calledOnce(fakeApi.profile.read); assert.calledOnce(fakeApi.profile.read);
}); });
});
it('should eventually expire the cache', () => { it('should eventually expire the cache', async () => {
clock = sinon.useFakeTimers(); const clock = sinon.useFakeTimers();
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
const session = createService(); const session = createService();
return session
.load() try {
.then(() => { await session.load();
clock.tick(CACHE_TTL * 2); clock.tick(CACHE_TTL * 2);
return session.load(); await session.load();
})
.then(() => {
assert.calledTwice(fakeApi.profile.read); assert.calledTwice(fakeApi.profile.read);
}); } finally {
clock.restore();
}
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment