vue-test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import Vue, { VNode } from "../index";
  2. import { ComponentOptions } from "../options";
  3. class Test extends Vue {
  4. a: number = 0;
  5. testProperties() {
  6. this.$data;
  7. this.$el;
  8. this.$options;
  9. this.$parent;
  10. this.$root;
  11. this.$children;
  12. this.$refs;
  13. this.$slots;
  14. this.$isServer;
  15. this.$ssrContext;
  16. this.$vnode;
  17. }
  18. // test property reification
  19. $el!: HTMLElement | SVGElement;
  20. $refs!: {
  21. vue: Vue,
  22. element: HTMLInputElement,
  23. vues: Vue[],
  24. elements: HTMLInputElement[]
  25. }
  26. testReification() {
  27. this.$refs.vue.$data;
  28. this.$refs.element.value;
  29. this.$refs.vues[0].$data;
  30. this.$refs.elements[0].value;
  31. }
  32. testMethods() {
  33. this.$mount("#app", false);
  34. this.$forceUpdate();
  35. this.$destroy();
  36. this.$set({}, "key", "value");
  37. this.$delete({}, "key");
  38. this.$watch("a", (val: number, oldVal: number) => {}, {
  39. immediate: true,
  40. deep: false
  41. })();
  42. this.$watch(() => this.a, (val: number) => {});
  43. this.$on("", () => {});
  44. this.$once("", () => {});
  45. this.$off("", () => {});
  46. this.$emit("", 1, 2, 3);
  47. this.$nextTick(function() {
  48. this.$nextTick;
  49. });
  50. this.$nextTick().then(() => {});
  51. this.$createElement("div", {}, "message");
  52. }
  53. static testConfig() {
  54. const { config } = this;
  55. config.silent;
  56. config.optionMergeStrategies;
  57. config.devtools;
  58. config.errorHandler = (err, vm) => {
  59. if (vm instanceof Test) {
  60. vm.testProperties();
  61. vm.testMethods();
  62. }
  63. };
  64. config.warnHandler = (msg, vm) => {
  65. if (vm instanceof Test) {
  66. vm.testProperties();
  67. vm.testMethods();
  68. }
  69. };
  70. config.keyCodes = { esc: 27 };
  71. config.ignoredElements = ['foo', /^ion-/];
  72. config.async = false
  73. }
  74. static testMethods() {
  75. this.extend({
  76. data() {
  77. return {
  78. msg: ""
  79. };
  80. }
  81. });
  82. this.nextTick(() => {});
  83. this.nextTick().then(() => {});
  84. this.set({}, "", "");
  85. this.set({}, 1, "");
  86. this.set([true, false, true], 1, true);
  87. this.delete({}, "");
  88. this.delete({}, 1);
  89. this.delete([true, false], 0);
  90. this.directive("", {bind() {}});
  91. this.filter("", (value: number) => value);
  92. this.component("", { data: () => ({}) });
  93. this.component("", { functional: true, render(h) { return h("div", "hello!") } });
  94. this.use;
  95. this.mixin(Test);
  96. this.compile("<div>{{ message }}</div>");
  97. this
  98. .use(() => {
  99. })
  100. .use(() => {
  101. })
  102. .mixin({})
  103. .mixin({});
  104. }
  105. }
  106. const HelloWorldComponent = Vue.extend({
  107. props: ["name"],
  108. data() {
  109. return {
  110. message: "Hello " + this.name,
  111. }
  112. },
  113. computed: {
  114. shouted(): string {
  115. return this.message.toUpperCase();
  116. }
  117. },
  118. methods: {
  119. getMoreExcited() {
  120. this.message += "!";
  121. }
  122. },
  123. watch: {
  124. message(a: string) {
  125. console.log(`Message ${this.message} was changed!`);
  126. }
  127. }
  128. });
  129. const FunctionalHelloWorldComponent = Vue.extend({
  130. functional: true,
  131. props: ["name"],
  132. render(createElement, ctxt) {
  133. return createElement("div", "Hello " + ctxt.props.name)
  134. }
  135. });
  136. const Parent = Vue.extend({
  137. data() {
  138. return { greeting: 'Hello' }
  139. }
  140. });
  141. const Child = Parent.extend({
  142. methods: {
  143. foo() {
  144. console.log(this.greeting.toLowerCase());
  145. }
  146. }
  147. });
  148. const GrandChild = Child.extend({
  149. computed: {
  150. lower(): string {
  151. return this.greeting.toLowerCase();
  152. }
  153. }
  154. });
  155. new GrandChild().lower.toUpperCase();
  156. for (let _ in (new Test()).$options) {
  157. }
  158. declare const options: ComponentOptions<Vue>;
  159. Vue.extend(options);
  160. Vue.component('test-comp', options);
  161. new Vue(options);
  162. // cyclic example
  163. Vue.extend({
  164. props: {
  165. bar: {
  166. type: String
  167. }
  168. },
  169. methods: {
  170. foo() {}
  171. },
  172. mounted () {
  173. this.foo()
  174. },
  175. // manual annotation
  176. render (h): VNode {
  177. const a = this.bar
  178. return h('canvas', {}, [a])
  179. }
  180. })