diff --git a/test/__snapshots__/loader.spec.js.snap b/test/__snapshots__/loader.spec.js.snap index 4a5d920..263e5b0 100644 --- a/test/__snapshots__/loader.spec.js.snap +++ b/test/__snapshots__/loader.spec.js.snap @@ -23,7 +23,30 @@ Object { } `; -exports[`Injects docgen result for non-SFC 1`] = ` +exports[`Injects docgen result for non-SFC with default exports 1`] = ` +Object { + "description": "", + "displayName": "basicDefault.vue", + "exportName": "default", + "props": Array [ + Object { + "defaultValue": Object { + "func": false, + "value": "-1", + }, + "description": "Foo the number.", + "name": "foo", + "tags": Object {}, + "type": Object { + "name": "number", + }, + }, + ], + "tags": Object {}, +} +`; + +exports[`Injects docgen result for non-SFC with named exports 1`] = ` Object { "description": "", "displayName": "basic.vue", @@ -89,3 +112,47 @@ Object { "tags": Object {}, } `; + +exports[`Injects docgen result non-SFC with multiple, mixed, both named and default, exports 1`] = ` +Object { + "description": "", + "exportName": "MyButton1", + "props": Array [ + Object { + "defaultValue": Object { + "func": false, + "value": "-1", + }, + "description": "Foo the number.", + "name": "foo", + "tags": Object {}, + "type": Object { + "name": "number", + }, + }, + ], + "tags": Object {}, +} +`; + +exports[`Injects docgen result non-SFC with multiple, mixed, both named and default, exports 2`] = ` +Object { + "description": "", + "exportName": "default", + "props": Array [ + Object { + "defaultValue": Object { + "func": false, + "value": "-1", + }, + "description": "Foo the number.", + "name": "foo", + "tags": Object {}, + "type": Object { + "name": "number", + }, + }, + ], + "tags": Object {}, +} +`; diff --git a/test/fixtures/basicDefault.vue.js b/test/fixtures/basicDefault.vue.js new file mode 100644 index 0000000..abe1918 --- /dev/null +++ b/test/fixtures/basicDefault.vue.js @@ -0,0 +1,14 @@ +import Vue from 'vue' + +export default { + props: { + /** + * Foo the number. + */ + foo: { + type: Number, + default: -1 + } + }, + template: '{{foo}}' +} diff --git a/test/fixtures/basicMixed.vue.js b/test/fixtures/basicMixed.vue.js new file mode 100644 index 0000000..bbb09f9 --- /dev/null +++ b/test/fixtures/basicMixed.vue.js @@ -0,0 +1,30 @@ +import Vue from 'vue' + +export const MyButton1 = { + props: { + /** + * Foo the number. + */ + foo: { + type: Number, + default: -1 + } + }, + template: '{{foo}}' +} + +export default { + props: { + /** + * Foo the number. + */ + foo: { + type: Number, + default: -1 + } + }, + template: '{{foo}}' +} + +// NOTE: vue-docgen-api cannot handle default exports (is it expected behavor or bug? idk...) +// export default MyButton diff --git a/test/loader.spec.js b/test/loader.spec.js index 5918cf7..d5e0ca1 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -10,7 +10,20 @@ it('Injects docgen result as __docgenInfo property', async () => { expect(JSON.parse(output.match(docgenPattern)[1])).toMatchSnapshot() }) -it('Injects docgen result for non-SFC', async () => { +it('Injects docgen result for non-SFC with default exports', async () => { + const fixture = './fixtures/basicDefault.vue.js' + + const stats = await compiler(fixture) + const output = stats.toJson().modules.find(mod => mod.name.includes(fixture)) + .source + + const docgenPattern = /\.__docgenInfo\s?=\s?(\{[\s\S]*})/ + + expect(output).toMatch(docgenPattern) + expect(JSON.parse(output.match(docgenPattern)[1])).toMatchSnapshot() +}) + +it('Injects docgen result for non-SFC with named exports', async () => { const fixture = './fixtures/basic.vue.js' const stats = await compiler(fixture) @@ -42,3 +55,23 @@ it('Injects docgen result non-SFC with multiple exports', async () => { expect(JSON.parse(output.match(match1)[1])).toMatchSnapshot() expect(JSON.parse(output.match(match2)[1])).toMatchSnapshot() }) + +it('Injects docgen result non-SFC with multiple, mixed, both named and default, exports', async () => { + const fixture = './fixtures/basicMixed.vue.js' + + const stats = await compiler(fixture) + const output = stats.toJson().modules.find(mod => mod.name.includes(fixture)) + .source + + const buttonExports = ['MyButton1', 'component'] + + const createDocgenPattern = exportName => + `^.*${exportName}.*\\.__docgenInfo\\s?=\\s?(\\{[\\s\\S]*?})$` + const match1 = new RegExp(createDocgenPattern(buttonExports[0]), 'm') + const match2 = new RegExp(createDocgenPattern(buttonExports[1]), 'm') + + expect(output).toMatch(match1) + expect(output).toMatch(match2) + expect(JSON.parse(output.match(match1)[1])).toMatchSnapshot() + expect(JSON.parse(output.match(match2)[1])).toMatchSnapshot() +})