vue-test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import Vue, { VNode } from "../index";
  2. import { ComponentOptions } from "../options";
  3. class Test extends Vue {
  4. a: number;
  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. }
  72. static testMethods() {
  73. this.extend({
  74. data() {
  75. return {
  76. msg: ""
  77. };
  78. }
  79. });
  80. this.nextTick(() => {});
  81. this.nextTick().then(() => {});
  82. this.set({}, "", "");
  83. this.set([true, false, true], 1, true);
  84. this.delete({}, "");
  85. this.delete([true, false], 0);
  86. this.directive("", {bind() {}});
  87. this.filter("", (value: number) => value);
  88. this.component("", { data: () => ({}) });
  89. this.component("", { functional: true, render(h) { return h("div", "hello!") } });
  90. this.use;
  91. this.mixin(Test);
  92. this.compile("<div>{{ message }}</div>");
  93. }
  94. }
  95. const HelloWorldComponent = Vue.extend({
  96. props: ["name"],
  97. data() {
  98. return {
  99. message: "Hello " + this.name,
  100. }
  101. },
  102. computed: {
  103. shouted(): string {
  104. return this.message.toUpperCase();
  105. }
  106. },
  107. methods: {
  108. getMoreExcited() {
  109. this.message += "!";
  110. }
  111. },
  112. watch: {
  113. message(a: string) {
  114. console.log(`Message ${this.message} was changed!`);
  115. }
  116. }
  117. });
  118. const FunctionalHelloWorldComponent = Vue.extend({
  119. functional: true,
  120. props: ["name"],
  121. render(createElement, ctxt) {
  122. return createElement("div", "Hello " + ctxt.props.name)
  123. }
  124. });
  125. const Parent = Vue.extend({
  126. data() {
  127. return { greeting: 'Hello' }
  128. }
  129. });
  130. const Child = Parent.extend({
  131. methods: {
  132. foo() {
  133. console.log(this.greeting.toLowerCase());
  134. }
  135. }
  136. });
  137. const GrandChild = Child.extend({
  138. computed: {
  139. lower(): string {
  140. return this.greeting.toLowerCase();
  141. }
  142. }
  143. });
  144. new GrandChild().lower.toUpperCase();
  145. for (let _ in (new Test()).$options) {
  146. }
  147. declare const options: ComponentOptions<Vue>;
  148. Vue.extend(options);
  149. Vue.component('test-comp', options);
  150. new Vue(options);
  151. // cyclic example
  152. Vue.extend({
  153. props: {
  154. bar: {
  155. type: String
  156. }
  157. },
  158. methods: {
  159. foo() {}
  160. },
  161. mounted () {
  162. this.foo()
  163. },
  164. // manual annotation
  165. render (h): VNode {
  166. const a = this.bar
  167. return h('canvas', {}, [a])
  168. }
  169. })