Ryan Chandler 5 年之前
父节点
当前提交
326c804bf2
共有 10 个文件被更改,包括 92 次插入638 次删除
  1. 0 18
      examples/cdn.html
  2. 0 17
      examples/index.html
  3. 0 10
      examples/store.js
  4. 0 2
      examples/test.js
  5. 6 0
      jest.config.js
  6. 0 1
      package.json
  7. 41 0
      tests/observable.spec.js
  8. 26 0
      tests/subscribe.spec.js
  9. 11 0
      tests/utils.spec.js
  10. 8 590
      yarn.lock

+ 0 - 18
examples/cdn.html

@@ -1,18 +0,0 @@
-<html>
-    <head>
-        <script src="../dist/spruce.umd.js"></script>
-        <script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"></script>
-    </head>
-    <body>
-        <div x-data x-subscribe>
-            <input type="text" x-model="$store.dropdown.hello">
-            <span x-text="$store.dropdown.hello"></span>
-        </div>
-        
-        <h3>The button below is a completely different component</h3>
-
-        <div x-data x-subscribe>
-            <button @click="$store.dropdown.hello = 'amazing'">Update world to amazing</button>
-        </div>
-    </body>
-</html>

+ 0 - 17
examples/index.html

@@ -1,17 +0,0 @@
-<html>
-    <head>
-        <script type="module" src="./test.js"></script>
-    </head>
-    <body>
-        <div x-data x-subscribe>
-            <input type="text" x-model="$store.dropdown.hello">
-            <span x-text="$store.dropdown.hello"></span>
-        </div>
-        
-        <h3>The button below is a completely different component</h3>
-
-        <div x-data x-subscribe>
-            <button @click="$store.dropdown.hello = null">Update world to amazing</button>
-        </div>
-    </body>
-</html>

+ 0 - 10
examples/store.js

@@ -1,10 +0,0 @@
-import Spruce from '../dist/spruce.module.js'
-
-Spruce.store('dropdown', {
-    hello: 'world',
-    testing: null,
-})
-
-Spruce.store('name', 'Application')
-
-export default Spruce

+ 0 - 2
examples/test.js

@@ -1,2 +0,0 @@
-import Store from './store.js'
-import '../node_modules/alpinejs/dist/alpine.js'

+ 6 - 0
jest.config.js

@@ -0,0 +1,6 @@
+module.exports = {
+    moduleDirectories: [
+        'node_modules',
+        'src'
+    ]
+}

+ 0 - 1
package.json

@@ -22,7 +22,6 @@
         "@testing-library/dom": "^7.2.2",
         "@testing-library/jest-dom": "^5.5.0",
         "alpinejs": "^2.3.1",
-        "ava": "^3.7.1",
         "jest": "^25.4.0",
         "jsdom-simulant": "^1.1.2",
         "microbundle": "^0.11.0",

+ 41 - 0
tests/observable.spec.js

@@ -0,0 +1,41 @@
+import { createObservable } from '../src/observable'
+
+test('createObservable > successfully wraps object', () => {
+    let target = {
+        foo: 'bar'
+    }
+
+    let observable = createObservable(target, () => {})
+
+    expect(observable.foo).toEqual('bar')
+})
+
+test('createObservable > can access deeply nested props', () => {
+    let target = {
+        foo: {
+            bar: {
+                baz: 'bob'
+            }
+        }
+    }
+
+    let observable = createObservable(target, () => {})
+
+    expect(observable.foo.bar.baz).toEqual('bob')
+})
+
+test('createObservable > will run callback on set trap', () => {
+    let target = {
+        foo: 'bar'
+    }
+
+    let fixture = 0
+
+    let observable = createObservable(target, () => {
+        fixture = 100
+    })
+
+    observable.foo = 'bob'
+
+    expect(fixture).toEqual(100)
+})

+ 26 - 0
tests/subscribe.spec.js

@@ -0,0 +1,26 @@
+import Spruce from '../dist/spruce'
+import { waitFor } from '@testing-library/dom'
+
+test('x-subscribe > correctly updates x-init directive', async () => {
+    document.body.innerHTML = `
+        <div x-subscribe></div>
+    `
+
+    Spruce.start()
+
+    await waitFor(() => {
+        expect(document.querySelector('div').getAttribute('x-init')).toEqual('$store = Spruce.subscribe($el)')
+    })
+})
+
+test('x-subscribe > correctly updates x-init when already defined', async () => {
+    document.body.innerHTML = `
+        <div x-subscribe x-init="testing = true"></div>
+    `
+
+    Spruce.start()
+
+    await waitFor(() => {
+        expect(document.querySelector('div').getAttribute('x-init')).toEqual('$store = Spruce.subscribe($el); testing = true')
+    })
+})

+ 11 - 0
tests/utils.spec.js

@@ -0,0 +1,11 @@
+import * as utils from '../src/utils'
+
+test('isNullOrUndefined > returns true for null and undefined', () => {
+    expect(utils.isNullOrUndefined(null)).toBeTruthy()
+    expect(utils.isNullOrUndefined(undefined)).toBeTruthy()
+})
+
+test('isNullOrUndefined > returns false for non null or undefined', () => {
+    expect(utils.isNullOrUndefined('')).toBeFalsy()
+    expect(utils.isNullOrUndefined(10)).toBeFalsy()
+})

文件差异内容过多而无法显示
+ 8 - 590
yarn.lock


部分文件因为文件数量过多而无法显示