File tree 3 files changed +90
-0
lines changed
test/runtime/samples/store-contextual 3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -122,8 +122,16 @@ export default class Expression {
122
122
const { name, nodes } = flattenReference ( node ) ;
123
123
124
124
if ( scope . has ( name ) ) return ;
125
+
125
126
if ( globalWhitelist . has ( name ) && ! component . var_lookup . has ( name ) ) return ;
126
127
128
+ if ( name [ 0 ] === '$' && template_scope . names . has ( name . slice ( 1 ) ) ) {
129
+ component . error ( node , {
130
+ code : `contextual-store` ,
131
+ message : `Stores must be declared at the top level of the component (this may change in a future version of Svelte)`
132
+ } ) ;
133
+ }
134
+
127
135
if ( template_scope . is_let ( name ) ) {
128
136
if ( ! function_expression ) {
129
137
dependencies . add ( name ) ;
Original file line number Diff line number Diff line change
1
+ import { writable } from '../../../../store.js' ;
2
+
3
+ const todos = [
4
+ writable ( { done : false , text : 'write docs' } ) ,
5
+ writable ( { done : false , text : 'implement contextual stores' } ) ,
6
+ writable ( { done : false , text : 'go outside' } )
7
+ ] ;
8
+
9
+ export default {
10
+ error : `Stores must be declared at the top level of the component (this may change in a future version of Svelte)` ,
11
+
12
+ props : {
13
+ todos
14
+ } ,
15
+
16
+ html : `
17
+ <label>
18
+ <input type=checkbox>
19
+ [todo] write docs
20
+ </label>
21
+
22
+ <label>
23
+ <input type=checkbox>
24
+ [todo] implement contextual stores
25
+ </label>
26
+
27
+ <label>
28
+ <input type=checkbox>
29
+ [todo] go outside
30
+ </label>
31
+ ` ,
32
+
33
+ async test ( { assert, component, target, window } ) {
34
+ const inputs = target . querySelectorAll ( 'input' ) ;
35
+ const change = new window . MouseEvent ( 'change' ) ;
36
+
37
+ inputs [ 1 ] . checked = true ;
38
+ await inputs [ 1 ] . dispatchEvent ( change ) ;
39
+
40
+ assert . htmlEqual ( target . innerHTML , `
41
+ <label>
42
+ <input type=checkbox>
43
+ [todo] write docs
44
+ </label>
45
+
46
+ <label>
47
+ <input type=checkbox>
48
+ [done] implement contextual stores
49
+ </label>
50
+
51
+ <label>
52
+ <input type=checkbox>
53
+ [todo] go outside
54
+ </label>
55
+ ` ) ;
56
+
57
+ await todos [ 0 ] . update ( todo => ( { done : ! todo . done , text : todo . text } ) ) ;
58
+
59
+ assert . htmlEqual ( target . innerHTML , `
60
+ <label>
61
+ <input type=checkbox>
62
+ [done] write docs
63
+ </label>
64
+
65
+ <label>
66
+ <input type=checkbox>
67
+ [done] implement contextual stores
68
+ </label>
69
+
70
+ <label>
71
+ <input type=checkbox>
72
+ [todo] go outside
73
+ </label>
74
+ ` ) ;
75
+ }
76
+ } ;
Original file line number Diff line number Diff line change
1
+ {#each todos as todo }
2
+ <label >
3
+ <input type =checkbox on:change ={e => todo .update (t => ({ done: e .target .checked , text: t .text }))}>
4
+ {$todo .done ? ' [done]' : ' [todo]' } {$todo .text }
5
+ </label >
6
+ {/each }
You can’t perform that action at this time.
0 commit comments