|
1 | 1 | import fs from 'fs' |
2 | 2 | import path from 'path' |
3 | 3 | import consola from 'consola' |
4 | | -import { serial as test } from 'ava' |
| 4 | +import { serial as test, before, after } from 'ava' |
5 | 5 | import config from '@/nuxt.config' |
6 | 6 | import { state as indexState } from '@/store/index' |
7 | 7 | import { state as examplesState } from '@/store/examples' |
8 | | -import { compilePlugin, removeCompiledPlugin } from '@/test/utils' |
| 8 | +import { compilePlugin, ioServerInit, removeCompiledPlugin } from '@/test/utils' |
9 | 9 |
|
10 | 10 | const { io } = config |
11 | 11 | const state = indexState() |
12 | 12 | state.examples = examplesState() |
| 13 | +state.examples.__ob__ = '' |
| 14 | +const src = path.resolve('./io/plugin.js') |
| 15 | +const tmpFile = path.resolve('./io/plugin.compiled.js') |
13 | 16 |
|
14 | | -test.only('Socket plugin', async (t) => { |
15 | | - consola.log('testing with plugin options', io) |
16 | | - const src = path.resolve('./io/plugin.js') |
17 | | - const tmpFile = path.resolve('./io/plugin.compiled.js') |
18 | | - const Plugin = await compilePlugin({ src, tmpFile, options: io }).catch( |
| 17 | +function loadPlugin(t, ioOpts = {}) { |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + const context = {} |
| 20 | + Plugin(context, (label, NuxtSocket) => { |
| 21 | + const testObj = { |
| 22 | + $store: { |
| 23 | + commit: (msg) => { |
| 24 | + consola.log('commit', msg) |
| 25 | + }, |
| 26 | + dispatch: (msg) => { |
| 27 | + consola.log('dispatch', msg) |
| 28 | + }, |
| 29 | + watch: (stateCb, dataCb) => { |
| 30 | + stateCb(state) |
| 31 | + dataCb({ sample: 123 }) |
| 32 | + } |
| 33 | + }, |
| 34 | + testSocket: NuxtSocket |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + const socket = testObj.testSocket(ioOpts) |
| 39 | + t.is(label, 'nuxtSocket') |
| 40 | + t.is(typeof NuxtSocket, 'function') |
| 41 | + t.is(NuxtSocket.name, 'nuxtSocket') |
| 42 | + t.is(socket.constructor.name, 'Socket') |
| 43 | + resolve(socket) |
| 44 | + } catch (e) { |
| 45 | + reject(e) |
| 46 | + } |
| 47 | + }) |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +let Plugin, pOptions |
| 52 | + |
| 53 | +before('Compile Plugin', async (t) => { |
| 54 | + const compiled = await compilePlugin({ src, tmpFile, options: io }).catch( |
19 | 55 | (err) => { |
20 | 56 | consola.error(err) |
21 | 57 | t.fail() |
22 | 58 | } |
23 | 59 | ) |
24 | | - const context = {} |
25 | | - Plugin(context, (label, NuxtSocket) => { |
26 | | - const testObj = { |
27 | | - $store: { |
28 | | - commit: (msg) => { |
29 | | - consola.log('commit', msg) |
30 | | - }, |
31 | | - dispatch: (msg) => { |
32 | | - consola.log('dispatch', msg) |
33 | | - }, |
34 | | - watch: (stateCb, dataCb) => { |
35 | | - stateCb(state) |
36 | | - dataCb({ sample: 123 }) |
| 60 | + Plugin = compiled.Plugin |
| 61 | + pOptions = compiled.pOptions |
| 62 | + t.pass() |
| 63 | +}) |
| 64 | + |
| 65 | +before('Init IO Server', ioServerInit) |
| 66 | + |
| 67 | +after('Remove compiled plugin', () => { |
| 68 | + removeCompiledPlugin(tmpFile) |
| 69 | +}) |
| 70 | + |
| 71 | +test('Socket plugin (empty options)', async (t) => { |
| 72 | + const testCfg = { sockets: [] } |
| 73 | + pOptions.set(testCfg) |
| 74 | + await loadPlugin(t).catch((e) => { |
| 75 | + t.is( |
| 76 | + e.message, |
| 77 | + "Please configure sockets if planning to use nuxt-socket-io: \r\n [{name: '', url: ''}]" |
| 78 | + ) |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
| 82 | +test('Socket plugin (malformed sockets)', async (t) => { |
| 83 | + const testCfg = { sockets: {} } |
| 84 | + pOptions.set(testCfg) |
| 85 | + await loadPlugin(t).catch((e) => { |
| 86 | + t.is( |
| 87 | + e.message, |
| 88 | + "Please configure sockets if planning to use nuxt-socket-io: \r\n [{name: '', url: ''}]" |
| 89 | + ) |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +test('Socket plugin (options missing info)', async (t) => { |
| 94 | + const testCfg = { sockets: [{}] } |
| 95 | + pOptions.set(testCfg) |
| 96 | + await loadPlugin(t).catch((e) => { |
| 97 | + t.is(e.message, 'URL must be defined for nuxtSocket') |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +test('Socket plugin (no vuex options)', async (t) => { |
| 102 | + const testCfg = { |
| 103 | + sockets: [ |
| 104 | + { |
| 105 | + name: 'home', |
| 106 | + default: true, |
| 107 | + url: 'http://localhost:3000' |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
| 111 | + pOptions.set(testCfg) |
| 112 | + await loadPlugin(t, { name: 'home' }) |
| 113 | +}) |
| 114 | + |
| 115 | +test('Socket plugin (malformed vuex options)', async (t) => { |
| 116 | + const testCfg = { |
| 117 | + sockets: [ |
| 118 | + { |
| 119 | + default: true, |
| 120 | + url: 'http://localhost:3000', |
| 121 | + vuex: { |
| 122 | + actions: {}, |
| 123 | + mutations: {}, |
| 124 | + emitBacks: {} |
37 | 125 | } |
38 | | - }, |
39 | | - testSocket: NuxtSocket |
40 | | - } |
41 | | - try { |
42 | | - const socket = testObj.testSocket({}) |
43 | | - t.is(label, 'nuxtSocket') |
44 | | - t.is(typeof NuxtSocket, 'function') |
45 | | - t.is(NuxtSocket.name, 'nuxtSocket') |
46 | | - t.is(socket.constructor.name, 'Socket') |
47 | | - } catch (e) { |
48 | | - consola.error( |
49 | | - 'Plugin error:', |
50 | | - e, |
51 | | - '\r\n\r\n(were mocks set up correctly?)' |
52 | | - ) |
53 | | - t.fail() |
54 | | - } |
| 126 | + } |
| 127 | + ] |
| 128 | + } |
| 129 | + pOptions.set(testCfg) |
| 130 | + await loadPlugin(t) |
| 131 | +}) |
| 132 | + |
| 133 | +test('Socket plugin (vuex opts as strings)', async (t) => { |
| 134 | + const testCfg = { |
| 135 | + sockets: [ |
| 136 | + { |
| 137 | + default: true, |
| 138 | + url: 'http://localhost:3000', |
| 139 | + vuex: { |
| 140 | + actions: ['someAction'], |
| 141 | + mutations: ['someMutation'] |
| 142 | + } |
| 143 | + } |
| 144 | + ] |
| 145 | + } |
| 146 | + pOptions.set(testCfg) |
| 147 | + await loadPlugin(t) |
| 148 | +}) |
| 149 | + |
| 150 | +test('Socket plugin (malformed emitBacks)', async (t) => { |
| 151 | + const emitBack = 'examples' |
| 152 | + const testCfg = { |
| 153 | + sockets: [ |
| 154 | + { |
| 155 | + default: true, |
| 156 | + url: 'http://localhost:3000', |
| 157 | + vuex: { |
| 158 | + actions: [], |
| 159 | + mutations: [], |
| 160 | + emitBacks: [emitBack] |
| 161 | + } |
| 162 | + } |
| 163 | + ] |
| 164 | + } |
| 165 | + pOptions.set(testCfg) |
| 166 | + await loadPlugin(t).catch((e) => { |
| 167 | + t.is( |
| 168 | + e.message, |
| 169 | + emitBack + ' is a vuex module. You probably want to watch its properties' |
| 170 | + ) |
| 171 | + }) |
| 172 | +}) |
| 173 | + |
| 174 | +test('Socket plugin (from nuxt.config)', async (t) => { |
| 175 | + delete process.env.TEST |
| 176 | + const { ioServer } = t.context |
| 177 | + const testSocket = await loadPlugin(t, { |
| 178 | + name: 'test', |
| 179 | + channel: '/index' |
| 180 | + }) |
| 181 | + const testJSON = { msg: 'it worked!' } |
| 182 | + const expected = 'It worked! Received msg: ' + JSON.stringify(testJSON) |
| 183 | + return new Promise((resolve) => { |
| 184 | + testSocket |
| 185 | + .emit('getMessage', testJSON, async (actual) => { |
| 186 | + t.is(expected, actual) |
| 187 | + await ioServer.stop() |
| 188 | + }) |
| 189 | + .on('disconnect', () => { |
| 190 | + resolve() |
| 191 | + }) |
55 | 192 | }) |
56 | | - removeCompiledPlugin(tmpFile) |
57 | 193 | }) |
0 commit comments