vue-test.ts 3.7 KB

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