From 6109cc14032fb6f649b51b3fb59051d1e1bee9dd Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 28 Jan 2018 18:56:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Test=20multiple=20promises?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/index.spec.js | 85 +++++++++++++++++++++++++++++++++---------- test/utils/Helper.vue | 12 +++++- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/test/index.spec.js b/test/index.spec.js index a2246da..ec0c1b4 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -5,29 +5,76 @@ import Helper from './utils/Helper' const tick = () => new Promise(resolve => setTimeout(resolve, 0)) describe('Promised', () => { - let wrapper, promise, resolve, reject - beforeEach(async () => { - [promise, resolve, reject] = await fakePromise() - wrapper = mount(Helper, { - propsData: { - promise, - }, + let wrapper + describe('single promise', () => { + let promise, resolve, reject + beforeEach(async () => { + [promise, resolve, reject] = await fakePromise() + wrapper = mount(Helper, { + propsData: { + promise, + }, + }) }) - }) - test('displays a loading screen waiting for the promise', async () => { - expect(wrapper.text()).toBe('loading') - }) + test('displays a loading screen waiting for the promise', async () => { + expect(wrapper.text()).toBe('loading') + }) + + test('displays the resolved value once resolved', async () => { + resolve('foo') + await tick() + expect(wrapper.text()).toBe('foo') + }) - test('displays the resolved value once resolved', async () => { - resolve('foo') - await tick() - expect(wrapper.text()).toBe('foo') + test('displays an error if rejected', async () => { + reject(new Error('hello')) + await tick() + expect(wrapper.text()).toBe('hello') + }) }) - test('displays an error if rejected', async () => { - reject(new Error('hello')) - await tick() - expect(wrapper.text()).toBe('hello') + describe('multiple promise', () => { + let fakedPromises + beforeEach(async () => { + fakedPromises = (await Promise.all( + Array.from({ length: 3 }, () => fakePromise()) + )).map(([promise, resolve, reject]) => ({ + promise, + resolve, + reject, + })) + + const promises = fakedPromises.map(({ promise }) => promise) + + wrapper = mount(Helper, { + propsData: { + promises, + }, + }) + }) + + test('displays a loading screen while no promise is resolved', async () => { + expect(wrapper.text()).toBe('loading') + }) + + test('displays the resolved values once resolved', async () => { + fakedPromises[0].resolve('foo') + await tick() + expect(wrapper.text()).toBe('foo') + }) + + test('displays errors if rejected', async () => { + fakedPromises[0].reject(new Error('failed')) + await tick() + expect(wrapper.text()).toBe('failed') + }) + + test('displays multiple errors if rejected', async () => { + fakedPromises[0].reject(new Error('one')) + fakedPromises[1].reject(new Error('two')) + await tick() + expect(wrapper.text()).toBe('one,two') + }) }) }) diff --git a/test/utils/Helper.vue b/test/utils/Helper.vue index e76480c..c96b877 100644 --- a/test/utils/Helper.vue +++ b/test/utils/Helper.vue @@ -2,9 +2,9 @@

loading

-

{{ data }}

+

{{ data | text }}

-

{{ error.message }}

+

{{ error | errorText }}

@@ -13,6 +13,14 @@ import Promised from '../../src' export default { props: ['promise', 'promises'], + filters: { + text (data) { + return Array.isArray(data) ? data.join(',') : data + }, + errorText (data) { + return Array.isArray(data) ? data.map(e => e.message).join(',') : data.message + } + }, components: { Promised }, }