vue-test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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(function () {
  84. console.log(this.text === 'test');
  85. }, { text: 'test'});
  86. this.nextTick().then(() => {});
  87. this.set({}, "", "");
  88. this.set({}, 1, "");
  89. this.set([true, false, true], 1, true);
  90. this.delete({}, "");
  91. this.delete({}, 1);
  92. this.delete([true, false], 0);
  93. this.directive("", {bind() {}});
  94. this.filter("", (value: number) => value);
  95. this.component("", { data: () => ({}) });
  96. this.component("", { functional: true, render(h) { return h("div", "hello!") } });
  97. this.use;
  98. this.mixin(Test);
  99. this.compile("<div>{{ message }}</div>");
  100. this
  101. .use(() => {
  102. })
  103. .use(() => {
  104. })
  105. .mixin({})
  106. .mixin({});
  107. }
  108. }
  109. const HelloWorldComponent = Vue.extend({
  110. props: ["name"],
  111. data() {
  112. return {
  113. message: "Hello " + this.name,
  114. }
  115. },
  116. computed: {
  117. shouted(): string {
  118. return this.message.toUpperCase();
  119. }
  120. },
  121. methods: {
  122. getMoreExcited() {
  123. this.message += "!";
  124. }
  125. },
  126. watch: {
  127. message(a: string) {
  128. console.log(`Message ${this.message} was changed!`);
  129. }
  130. }
  131. });
  132. const FunctionalHelloWorldComponent = Vue.extend({
  133. functional: true,
  134. props: ["name"],
  135. render(createElement, ctxt) {
  136. return createElement("div", "Hello " + ctxt.props.name)
  137. }
  138. });
  139. const FunctionalScopedSlotsComponent = Vue.extend({
  140. functional: true,
  141. render(h, ctx) {
  142. return ctx.scopedSlots.default && ctx.scopedSlots.default({}) || h('div', 'functional scoped slots');
  143. }
  144. });
  145. const Parent = Vue.extend({
  146. data() {
  147. return { greeting: 'Hello' }
  148. }
  149. });
  150. const Child = Parent.extend({
  151. methods: {
  152. foo() {
  153. console.log(this.greeting.toLowerCase());
  154. }
  155. }
  156. });
  157. const GrandChild = Child.extend({
  158. computed: {
  159. lower(): string {
  160. return this.greeting.toLowerCase();
  161. }
  162. }
  163. });
  164. new GrandChild().lower.toUpperCase();
  165. for (let _ in (new Test()).$options) {
  166. }
  167. declare const options: ComponentOptions<Vue>;
  168. Vue.extend(options);
  169. Vue.component('test-comp', options);
  170. new Vue(options);
  171. // cyclic example
  172. Vue.extend({
  173. props: {
  174. bar: {
  175. type: String
  176. }
  177. },
  178. methods: {
  179. foo() {}
  180. },
  181. mounted () {
  182. this.foo()
  183. },
  184. // manual annotation
  185. render (h): VNode {
  186. const a = this.bar
  187. return h('canvas', {}, [a])
  188. }
  189. })
  190. declare function decorate<VC extends typeof Vue>(v: VC): VC;
  191. @decorate
  192. class Decorated extends Vue {
  193. a = 123;
  194. }
  195. const obj = Vue.observable({ a: 1 })
  196. obj.a++
  197. // VNodeData style tests.
  198. const ComponentWithStyleInVNodeData = Vue.extend({
  199. render (h) {
  200. const elementWithStyleAsString = h('div', {
  201. style: 'background-color: red;'
  202. });
  203. const elementWithStyleAsObject = h('div', {
  204. style: { backgroundColor: 'green' }
  205. });
  206. const elementWithStyleAsArrayOfObjects = h('div', {
  207. style: [
  208. { backgroundColor: 'blue' }
  209. ]
  210. });
  211. return h('div', undefined, [
  212. elementWithStyleAsString,
  213. elementWithStyleAsObject,
  214. elementWithStyleAsArrayOfObjects
  215. ]);
  216. }
  217. });