/test/ClassComponent.spec.js

https://github.com/vuedoc/parser · JavaScript · 691 lines · 556 code · 43 blank · 92 comment · 0 complexity · ba072b7f83a34ec2bf27370f89601cd9 MD5 · raw file

  1. const { ComponentTestCase } = require('./lib/TestUtils');
  2. /* eslint-disable max-len */
  3. /* eslint-disable indent */
  4. /* global describe */
  5. describe('Class Component', () => {
  6. ComponentTestCase({
  7. name: '#37 - Class Component',
  8. options: {
  9. filecontent: `
  10. <script>
  11. import Vue from 'vue';
  12. import Component from 'vue-class-component';
  13. /**
  14. * A class component element
  15. *
  16. * @contributor Jon Snow
  17. */
  18. @Component({
  19. inheritAttrs: false,
  20. props: {
  21. /**
  22. * prop name description
  23. */
  24. name: String
  25. }
  26. })
  27. class App extends Vue {
  28. static staticVar = 123;
  29. static ignoredMethod() {}
  30. constructor() {
  31. super(...arguments);
  32. /**
  33. * data msg description
  34. */
  35. this.msg = 'Hello';
  36. /**
  37. * data helloMsg with expression
  38. */
  39. this.helloMsg = 'Hello, ' + this.name;
  40. /**
  41. * event constructor description
  42. */
  43. this.$emit('created')
  44. }
  45. // lifecycle hook
  46. mounted() {
  47. this.greet();
  48. /**
  49. * event mounted description
  50. */
  51. this.$emit('mounted')
  52. }
  53. /**
  54. * computed computedMsg description
  55. */
  56. get computedMsg() {
  57. return 'computed ' + this.msg;
  58. }
  59. /**
  60. * computed computedString description
  61. */
  62. get computedString() {
  63. return \`computed \` + this.msg;
  64. }
  65. /**
  66. * computed computedString description
  67. */
  68. get computedString2() {
  69. return 'computed';
  70. }
  71. /**
  72. * computed computedNull description
  73. */
  74. get computedNull() {
  75. return null;
  76. }
  77. /**
  78. * computed computedBool description
  79. */
  80. get computedBool() {
  81. return true;
  82. }
  83. /**
  84. * computed computedArray description
  85. */
  86. get computedArray() {
  87. return [1, 2];
  88. }
  89. /**
  90. * computed computedObject description
  91. */
  92. get computedObject() {
  93. return {};
  94. }
  95. /**
  96. * computed computedNewObject description
  97. */
  98. get computedNewObject() {
  99. return new Array();
  100. }
  101. /**
  102. * computed CustomeObject description
  103. */
  104. get computedCustomeObject() {
  105. return new CustomeObject();
  106. }
  107. /**
  108. * computed computedNewObject2 description
  109. */
  110. get computedNewObject2() {
  111. return new hash.val();
  112. }
  113. /**
  114. * computed computedRegExpLiteral description
  115. */
  116. get computedRegExpLiteral() {
  117. return /[a-z]/;
  118. }
  119. /**
  120. * computed computedBigIntLiteral description
  121. */
  122. get computedBigIntLiteral() {
  123. return 100n;
  124. }
  125. /**
  126. * computed computedNumber description
  127. */
  128. get computedNumber() {
  129. return 12;
  130. }
  131. /**
  132. * computed computedNumber description
  133. */
  134. get computedNumber1() {
  135. return !12;
  136. }
  137. /**
  138. * computed computedNumber description
  139. */
  140. get computedNumber2() {
  141. return 12 + this.msg;
  142. }
  143. /**
  144. * computed [Symbol.species] description
  145. */
  146. get [Symbol.species]() { return Array; }
  147. /**
  148. * computed [Symbol.species] description
  149. */
  150. get empty() {}
  151. public get something(): Array<any> {
  152. return [];
  153. }
  154. /**
  155. * method greet description
  156. */
  157. greet() {
  158. alert('greeting: ' + this.msg);
  159. }
  160. /**
  161. * method _protectedMethod description
  162. * @protected
  163. */
  164. _protectedMethod() {}
  165. /**
  166. * method _ignoredMethod description
  167. * @private
  168. */
  169. _ignoredMethod() {}
  170. };
  171. export default App;
  172. </script>
  173. `,
  174. ignoredVisibilities: [ 'private' ]
  175. },
  176. expected: {
  177. name: 'App',
  178. description: 'A class component element',
  179. inheritAttrs: false,
  180. errors: [],
  181. keywords: [
  182. {
  183. name: 'contributor',
  184. description: 'Jon Snow'
  185. }
  186. ],
  187. events: [
  188. { kind: 'event',
  189. name: 'created',
  190. arguments: [],
  191. description: 'event constructor description',
  192. category: undefined,
  193. version: undefined,
  194. keywords: [],
  195. visibility: 'public'
  196. },
  197. { kind: 'event',
  198. name: 'mounted',
  199. arguments: [],
  200. description: 'event mounted description',
  201. category: undefined,
  202. version: undefined,
  203. keywords: [],
  204. visibility: 'public'
  205. }
  206. ],
  207. methods: [
  208. { kind: 'method',
  209. name: 'greet',
  210. params: [],
  211. description: 'method greet description',
  212. category: undefined,
  213. version: undefined,
  214. keywords: [],
  215. syntax: [
  216. 'greet(): void'
  217. ],
  218. visibility: 'public',
  219. returns: {
  220. description: undefined,
  221. type: 'void'
  222. }
  223. },
  224. { kind: 'method',
  225. name: '_protectedMethod',
  226. params: [],
  227. description: 'method _protectedMethod description',
  228. category: undefined,
  229. version: undefined,
  230. keywords: [],
  231. syntax: [
  232. '_protectedMethod(): void'
  233. ],
  234. visibility: 'protected',
  235. returns: {
  236. description: undefined,
  237. type: 'void'
  238. }
  239. }
  240. ],
  241. computed: [
  242. { kind: 'computed',
  243. name: 'computedMsg',
  244. type: 'string',
  245. category: undefined,
  246. version: undefined,
  247. dependencies: [ 'msg' ],
  248. description: 'computed computedMsg description',
  249. keywords: [],
  250. visibility: 'public'
  251. },
  252. { kind: 'computed',
  253. name: 'computedString',
  254. type: 'string',
  255. category: undefined,
  256. version: undefined,
  257. dependencies: [ 'msg' ],
  258. description: 'computed computedString description',
  259. keywords: [],
  260. visibility: 'public'
  261. },
  262. { kind: 'computed',
  263. name: 'computedString2',
  264. type: 'string',
  265. category: undefined,
  266. version: undefined,
  267. dependencies: [],
  268. description: 'computed computedString description',
  269. keywords: [],
  270. visibility: 'public'
  271. },
  272. { kind: 'computed',
  273. name: 'computedNull',
  274. type: 'unknown',
  275. category: undefined,
  276. version: undefined,
  277. dependencies: [],
  278. description: 'computed computedNull description',
  279. keywords: [],
  280. visibility: 'public'
  281. },
  282. { kind: 'computed',
  283. name: 'computedBool',
  284. type: 'boolean',
  285. category: undefined,
  286. version: undefined,
  287. dependencies: [],
  288. description: 'computed computedBool description',
  289. keywords: [],
  290. visibility: 'public'
  291. },
  292. { kind: 'computed',
  293. name: 'computedArray',
  294. type: 'array',
  295. category: undefined,
  296. version: undefined,
  297. dependencies: [],
  298. description: 'computed computedArray description',
  299. keywords: [],
  300. visibility: 'public'
  301. },
  302. { kind: 'computed',
  303. name: 'computedObject',
  304. type: 'object',
  305. category: undefined,
  306. version: undefined,
  307. dependencies: [],
  308. description: 'computed computedObject description',
  309. keywords: [],
  310. visibility: 'public'
  311. },
  312. { kind: 'computed',
  313. name: 'computedNewObject',
  314. type: 'array',
  315. category: undefined,
  316. version: undefined,
  317. dependencies: [],
  318. description: 'computed computedNewObject description',
  319. keywords: [],
  320. visibility: 'public'
  321. },
  322. { kind: 'computed',
  323. name: 'computedCustomeObject',
  324. type: 'CustomeObject',
  325. category: undefined,
  326. version: undefined,
  327. dependencies: [],
  328. description: 'computed CustomeObject description',
  329. keywords: [],
  330. visibility: 'public'
  331. },
  332. { kind: 'computed',
  333. name: 'computedNewObject2',
  334. type: 'object',
  335. category: undefined,
  336. version: undefined,
  337. dependencies: [],
  338. description: 'computed computedNewObject2 description',
  339. keywords: [],
  340. visibility: 'public'
  341. },
  342. { kind: 'computed',
  343. name: 'computedRegExpLiteral',
  344. type: 'regexp',
  345. category: undefined,
  346. version: undefined,
  347. dependencies: [],
  348. description: 'computed computedRegExpLiteral description',
  349. keywords: [],
  350. visibility: 'public'
  351. },
  352. { kind: 'computed',
  353. name: 'computedBigIntLiteral',
  354. type: 'bigint',
  355. category: undefined,
  356. version: undefined,
  357. dependencies: [],
  358. description: 'computed computedBigIntLiteral description',
  359. keywords: [],
  360. visibility: 'public'
  361. },
  362. { kind: 'computed',
  363. name: 'computedNumber',
  364. type: 'number',
  365. category: undefined,
  366. version: undefined,
  367. dependencies: [],
  368. description: 'computed computedNumber description',
  369. keywords: [],
  370. visibility: 'public'
  371. },
  372. { kind: 'computed',
  373. name: 'computedNumber1',
  374. type: 'boolean',
  375. category: undefined,
  376. version: undefined,
  377. dependencies: [],
  378. description: 'computed computedNumber description',
  379. keywords: [],
  380. visibility: 'public'
  381. },
  382. { kind: 'computed',
  383. name: 'computedNumber2',
  384. type: 'number',
  385. category: undefined,
  386. version: undefined,
  387. dependencies: [ 'msg' ],
  388. description: 'computed computedNumber description',
  389. keywords: [],
  390. visibility: 'public'
  391. },
  392. { kind: 'computed',
  393. name: '[Symbol.species]',
  394. type: 'unknown',
  395. category: undefined,
  396. version: undefined,
  397. dependencies: [],
  398. description: 'computed [Symbol.species] description',
  399. keywords: [],
  400. visibility: 'public'
  401. },
  402. { kind: 'computed',
  403. name: 'empty',
  404. type: 'unknown',
  405. category: undefined,
  406. version: undefined,
  407. dependencies: [],
  408. description: 'computed [Symbol.species] description',
  409. keywords: [],
  410. visibility: 'public'
  411. },
  412. { kind: 'computed',
  413. name: 'something',
  414. type: 'Array<any>',
  415. category: undefined,
  416. version: undefined,
  417. dependencies: [],
  418. description: undefined,
  419. keywords: [],
  420. visibility: 'public'
  421. }
  422. ],
  423. data: [
  424. {
  425. kind: 'data',
  426. name: 'msg',
  427. type: 'string',
  428. category: undefined,
  429. version: undefined,
  430. description: 'data msg description',
  431. initialValue: '"Hello"',
  432. keywords: [],
  433. visibility: 'public'
  434. },
  435. {
  436. kind: 'data',
  437. name: 'helloMsg',
  438. type: 'string',
  439. category: undefined,
  440. version: undefined,
  441. description: 'data helloMsg with expression',
  442. initialValue: '\'Hello, \' + this.name',
  443. keywords: [],
  444. visibility: 'public'
  445. }
  446. ],
  447. props: [
  448. { kind: 'prop',
  449. name: 'name',
  450. type: 'String',
  451. category: undefined,
  452. version: undefined,
  453. required: false,
  454. default: undefined,
  455. describeModel: false,
  456. description: 'prop name description',
  457. keywords: [],
  458. visibility: 'public'
  459. }
  460. ],
  461. slots: []
  462. }
  463. });
  464. ComponentTestCase({
  465. name: 'vuedoc.md#25 - Class Component',
  466. options: {
  467. filecontent: `
  468. <script>
  469. import Vue from 'vue';
  470. import Component from 'vue-class-component';
  471. /**
  472. * MyComponent description
  473. */
  474. @Component({
  475. name: 'MyComponent',
  476. router,
  477. components: {
  478. XHeader,
  479. },
  480. })
  481. class App extends Vue {
  482. data() {
  483. return {
  484. routeQueue: [this.$router.currentRoute],
  485. historyLength: window.history.length,
  486. transitionName: 'slide-left',
  487. init: false,
  488. };
  489. }
  490. };
  491. export default App
  492. </script>
  493. `
  494. },
  495. expected: {
  496. name: 'MyComponent',
  497. description: 'MyComponent description',
  498. inheritAttrs: true,
  499. errors: [],
  500. keywords: [],
  501. events: [],
  502. methods: [],
  503. computed: [],
  504. data: [
  505. {
  506. kind: 'data',
  507. name: 'routeQueue',
  508. type: 'array',
  509. description: undefined,
  510. category: undefined,
  511. version: undefined,
  512. initialValue: '[this.$router.currentRoute]',
  513. keywords: [],
  514. visibility: 'public'
  515. },
  516. {
  517. kind: 'data',
  518. name: 'historyLength',
  519. type: 'object',
  520. description: undefined,
  521. category: undefined,
  522. version: undefined,
  523. initialValue: 'window.history.length',
  524. keywords: [],
  525. visibility: 'public'
  526. },
  527. {
  528. kind: 'data',
  529. name: 'transitionName',
  530. type: 'string',
  531. description: undefined,
  532. category: undefined,
  533. version: undefined,
  534. initialValue: '"slide-left"',
  535. keywords: [],
  536. visibility: 'public'
  537. },
  538. {
  539. kind: 'data',
  540. name: 'init',
  541. type: 'boolean',
  542. description: undefined,
  543. category: undefined,
  544. version: undefined,
  545. initialValue: 'false',
  546. keywords: [],
  547. visibility: 'public'
  548. },
  549. ],
  550. props: [],
  551. slots: []
  552. }
  553. });
  554. ComponentTestCase({
  555. name: 'Class Component Visibilities',
  556. options: {
  557. filecontent: `
  558. <script>
  559. import Vue from 'vue';
  560. import Component from 'vue-class-component';
  561. /**
  562. * MyComponent description
  563. */
  564. @Component()
  565. export default class MyComponent extends Vue {
  566. private shouldBeIgnored = 'some string';
  567. protected shouldBeIgnored2 = 'some other string';
  568. #shouldBeIgnored3 = 'some other string';
  569. public shouldBeSeen = 'some other string';
  570. private get shouldBeIgnored3() {
  571. return 'some other string';
  572. }
  573. protected get shouldBeIgnored4() {
  574. return 'some other string';
  575. }
  576. get #shouldBeIgnored5() {
  577. return 'some other string';
  578. }
  579. public get shouldBeSeen2() {
  580. return 'some other string';
  581. }
  582. #shouldBeIgnored6() {
  583. return 'some other string';
  584. }
  585. public shouldBeSeen3() {
  586. return 'some other string';
  587. }
  588. }
  589. </script>
  590. `
  591. },
  592. expected: {
  593. name: 'MyComponent',
  594. description: 'MyComponent description',
  595. inheritAttrs: true,
  596. errors: [],
  597. keywords: [],
  598. events: [],
  599. methods: [
  600. {
  601. kind: 'method',
  602. name: 'shouldBeSeen3',
  603. description: undefined,
  604. category: undefined,
  605. version: undefined,
  606. keywords: [],
  607. params: [],
  608. visibility: 'public',
  609. returns: {
  610. type: 'void',
  611. description: undefined,
  612. },
  613. syntax: [
  614. 'shouldBeSeen3(): void',
  615. ],
  616. },
  617. ],
  618. computed: [
  619. {
  620. kind: 'computed',
  621. name: 'shouldBeSeen2',
  622. type: 'string',
  623. description: undefined,
  624. category: undefined,
  625. version: undefined,
  626. keywords: [],
  627. visibility: 'public',
  628. dependencies: [],
  629. },
  630. ],
  631. data: [
  632. {
  633. kind: 'data',
  634. name: 'shouldBeSeen',
  635. type: 'string',
  636. description: undefined,
  637. category: undefined,
  638. version: undefined,
  639. initialValue: '"some other string"',
  640. keywords: [],
  641. visibility: 'public'
  642. },
  643. ],
  644. props: [],
  645. slots: []
  646. }
  647. });
  648. });