]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: Add tests for some runtime helpers (#87)
authorJordan Pittman <jordan@cryptica.me>
Sun, 6 Oct 2019 03:22:42 +0000 (23:22 -0400)
committerEvan You <yyx990803@gmail.com>
Sun, 6 Oct 2019 03:22:42 +0000 (23:22 -0400)
* test for `renderList`
* test for `toHandlers`

packages/runtime-core/__tests__/helpers/renderList.spec.ts [new file with mode: 0644]
packages/runtime-core/__tests__/helpers/toHandlers.spec.ts [new file with mode: 0644]
packages/runtime-test/src/jestUtils.ts

diff --git a/packages/runtime-core/__tests__/helpers/renderList.spec.ts b/packages/runtime-core/__tests__/helpers/renderList.spec.ts
new file mode 100644 (file)
index 0000000..d55bffb
--- /dev/null
@@ -0,0 +1,44 @@
+import { renderList } from '../../src/helpers/renderList'
+
+describe('renderList', () => {
+  it('should render items in an array', () => {
+    expect(
+      renderList(['1', '2', '3'], (item, index) => `node ${index}: ${item}`)
+    ).toEqual(['node 0: 1', 'node 1: 2', 'node 2: 3'])
+  })
+
+  it('should render characters of a string', () => {
+    expect(
+      renderList('123', (item, index) => `node ${index}: ${item}`)
+    ).toEqual(['node 0: 1', 'node 1: 2', 'node 2: 3'])
+  })
+
+  it('should render integers 1 through N when given a number N', () => {
+    expect(renderList(3, (item, index) => `node ${index}: ${item}`)).toEqual([
+      'node 0: 1',
+      'node 1: 2',
+      'node 2: 3'
+    ])
+  })
+
+  it('should render properties in an object', () => {
+    expect(
+      renderList(
+        { a: 1, b: 2, c: 3 },
+        (item, key, index) => `node ${index}/${key}: ${item}`
+      )
+    ).toEqual(['node 0/a: 1', 'node 1/b: 2', 'node 2/c: 3'])
+  })
+
+  it('should render an item for entry in an iterable', () => {
+    const iterable = function*() {
+      yield 1
+      yield 2
+      yield 3
+    }
+
+    expect(
+      renderList(iterable(), (item, index) => `node ${index}: ${item}`)
+    ).toEqual(['node 0: 1', 'node 1: 2', 'node 2: 3'])
+  })
+})
diff --git a/packages/runtime-core/__tests__/helpers/toHandlers.spec.ts b/packages/runtime-core/__tests__/helpers/toHandlers.spec.ts
new file mode 100644 (file)
index 0000000..a71ef38
--- /dev/null
@@ -0,0 +1,25 @@
+import { toHandlers } from '../../src/helpers/toHandlers'
+import { mockWarn } from '@vue/runtime-test'
+
+describe('toHandlers', () => {
+  mockWarn()
+
+  it('should not accept non-objects', () => {
+    toHandlers((null as unknown) as any)
+    toHandlers((undefined as unknown) as any)
+
+    expect(
+      'v-on with no argument expects an object value.'
+    ).toHaveBeenWarnedTimes(2)
+  })
+
+  it('should properly change object keys', () => {
+    const input = () => {}
+    const change = () => {}
+
+    expect(toHandlers({ input, change })).toStrictEqual({
+      oninput: input,
+      onchange: change
+    })
+  })
+})
index 6ebbcb91bcc244897ba29c384fc8cbce54dd637e..6e488fb9ac0b748b680fadea599275e315cc6869 100644 (file)
@@ -57,11 +57,12 @@ export function mockWarn() {
           found++
         }
       })
-      if (found > 0) {
+
+      if (found === n) {
         return {
           pass: true,
           message: () =>
-            `expected "${received}" not to have been warned ${n} times.`
+            `expected "${received}" to have been warned ${n} times.`
         }
       } else {
         return {