11import assert from "assert" ;
22import sinon from "sinon" ;
3+ import chai from "chai" ;
34
45import { Digital } from "@dtex/mock-io" ;
56import Button from "j5e/button" ;
@@ -16,6 +17,103 @@ describe("Button", function() {
1617 assert . equal ( button instanceof Button , true ) ;
1718 assert . equal ( button . io instanceof Digital , true ) ;
1819 } ) ;
20+
21+ describe ( "Options" , function ( ) {
22+
23+ describe ( "invert" , function ( ) {
24+
25+ it ( "should invert state when the invert option is true" , async function ( ) {
26+ const button = await new Button ( {
27+ pin : 13 ,
28+ io : Digital ,
29+ invert : true
30+ } ) ;
31+
32+ button . io . write ( 0 ) ;
33+ assert . equal ( button . isClosed , true ) ;
34+ assert . equal ( button . isOpen , false ) ;
35+
36+ button . io . write ( 1 ) ;
37+ assert . equal ( button . isClosed , false ) ;
38+ assert . equal ( button . isOpen , true ) ;
39+
40+ } ) ;
41+ } ) ;
42+
43+ describe ( "isPullup" , function ( ) {
44+
45+ it ( "should invert state when the isPullup option is true" , async function ( ) {
46+ const button = await new Button ( {
47+ pin : 13 ,
48+ io : Digital ,
49+ isPullup : true
50+ } ) ;
51+
52+ button . io . write ( 0 ) ;
53+ assert . equal ( button . isClosed , true ) ;
54+ assert . equal ( button . isOpen , false ) ;
55+
56+ button . io . write ( 1 ) ;
57+ assert . equal ( button . isClosed , false ) ;
58+ assert . equal ( button . isOpen , true ) ;
59+
60+ } ) ;
61+
62+ it ( "should not invert state when the isPullup and invert options are true" , async function ( ) {
63+ const button = await new Button ( {
64+ pin : 13 ,
65+ io : Digital ,
66+ isPullup : true ,
67+ invert : true
68+ } ) ;
69+
70+ button . io . write ( 1 ) ;
71+ assert . equal ( button . isClosed , true ) ;
72+ assert . equal ( button . isOpen , false ) ;
73+
74+ button . io . write ( 0 ) ;
75+ assert . equal ( button . isClosed , false ) ;
76+ assert . equal ( button . isOpen , true ) ;
77+
78+ } ) ;
79+
80+ } ) ;
81+
82+ describe ( "isPulldown" , function ( ) {
83+
84+ it ( "should not invert state when the isPulldown option is true" , async function ( ) {
85+ const button = await new Button ( {
86+ pin : 13 ,
87+ io : Digital ,
88+ isPulldown : true
89+ } ) ;
90+
91+ button . io . write ( 1 ) ;
92+ assert . equal ( button . isClosed , true ) ;
93+ assert . equal ( button . isOpen , false ) ;
94+
95+ button . io . write ( 0 ) ;
96+ assert . equal ( button . isClosed , false ) ;
97+ assert . equal ( button . isOpen , true ) ;
98+
99+ } ) ;
100+ } ) ;
101+
102+ describe ( "holdtime" , function ( ) {
103+
104+ it ( "should set the holdtime state when passed a value in options" , async function ( ) {
105+ const button = await new Button ( {
106+ pin : 13 ,
107+ io : Digital ,
108+ holdtime : 2000
109+ } ) ;
110+
111+ assert . equal ( button . holdtime , 2000 ) ;
112+
113+ } ) ;
114+ } ) ;
115+
116+ } ) ;
19117 } ) ;
20118
21119 describe ( "Properties" , function ( ) {
@@ -70,6 +168,198 @@ describe("Button", function() {
70168 assert . equal ( button . isOpen , true ) ;
71169 } ) ;
72170 } ) ;
171+
172+ describe ( "holdtime" , function ( ) {
173+
174+ it ( "should be settable and getable" , async function ( ) {
175+ const button = await new Button ( {
176+ pin : 13 ,
177+ io : Digital
178+ } ) ;
179+
180+ assert . equal ( button . holdtime , 500 ) ;
181+ button . holdtime = 1000 ;
182+ assert . equal ( button . holdtime , 1000 ) ;
183+
184+ } ) ;
185+
186+ it ( "should emit hold event after 500ms" , async function ( ) {
187+
188+ const clock = sinon . useFakeTimers ( ) ;
189+ const holdSpy = sinon . spy ( ) ;
190+ const button = await new Button ( {
191+ pin : 13 ,
192+ io : Digital
193+ } ) ;
194+
195+ button . on ( "hold" , holdSpy ) ;
196+
197+ clock . tick ( 1 ) ;
198+ button . io . write ( 1 ) ;
199+ assert . equal ( holdSpy . callCount , 0 ) ;
200+ clock . tick ( 499 ) ;
201+ assert . equal ( holdSpy . callCount , 0 ) ;
202+ clock . tick ( 10 ) ;
203+ assert . equal ( holdSpy . callCount , 1 ) ;
204+
205+ clock . restore ( ) ;
206+
207+ } ) ;
208+
209+ it ( "should emit hold event after 2000ms" , async function ( ) {
210+
211+ const clock = sinon . useFakeTimers ( ) ;
212+ const holdSpy = sinon . spy ( ) ;
213+ const button = await new Button ( {
214+ pin : 13 ,
215+ io : Digital ,
216+ holdtime : 2000
217+ } ) ;
218+
219+ button . on ( "hold" , holdSpy ) ;
220+
221+ clock . tick ( 1 ) ;
222+ button . io . write ( 1 ) ;
223+ assert . equal ( holdSpy . callCount , 0 ) ;
224+ clock . tick ( 1999 ) ;
225+ assert . equal ( holdSpy . callCount , 0 ) ;
226+ clock . tick ( 10 ) ;
227+ assert . equal ( holdSpy . callCount , 1 ) ;
228+
229+ clock . restore ( ) ;
230+
231+ } ) ;
232+
233+
234+
235+ } ) ;
236+
237+ describe ( "downValue" , function ( ) {
238+
239+ it ( "should have the correct default value" , async function ( ) {
240+
241+ const button = await new Button ( {
242+ pin : 13 ,
243+ io : Digital
244+ } ) ;
245+
246+ assert . equal ( button . downValue , 1 ) ;
247+
248+ } ) ;
249+
250+ it ( "should have the correct value when invert is true" , async function ( ) {
251+
252+ const button = await new Button ( {
253+ pin : 13 ,
254+ io : Digital ,
255+ invert : true
256+ } ) ;
257+
258+ assert . equal ( button . downValue , 0 ) ;
259+
260+ } ) ;
261+
262+ it ( "should have the correct value when isPullup is true" , async function ( ) {
263+
264+ const button = await new Button ( {
265+ pin : 13 ,
266+ io : Digital ,
267+ isPullup : true
268+ } ) ;
269+
270+ assert . equal ( button . downValue , 0 ) ;
271+
272+ } ) ;
273+
274+ it ( "should have the correct value when isPullup and invert are true" , async function ( ) {
275+
276+ const button = await new Button ( {
277+ pin : 13 ,
278+ io : Digital ,
279+ isPullup : true ,
280+ invert : true
281+ } ) ;
282+
283+ assert . equal ( button . downValue , 1 ) ;
284+
285+ } ) ;
286+
287+ it ( "should not be settable" , async function ( ) {
288+ const button = await new Button ( {
289+ pin : 13 ,
290+ io : Digital
291+ } ) ;
292+
293+ chai . expect ( ( ) => {
294+ button . downValue = 1 ;
295+ } ) . to . throw ( TypeError ) ;
296+ } ) ;
297+
298+ } ) ;
299+
300+ describe ( "upValue" , function ( ) {
301+
302+ it ( "should have the correct default value" , async function ( ) {
303+
304+ const button = await new Button ( {
305+ pin : 13 ,
306+ io : Digital
307+ } ) ;
308+
309+ assert . equal ( button . upValue , 0 ) ;
310+
311+ } ) ;
312+
313+ it ( "should have the correct value when invert is true" , async function ( ) {
314+
315+ const button = await new Button ( {
316+ pin : 13 ,
317+ io : Digital ,
318+ invert : true
319+ } ) ;
320+
321+ assert . equal ( button . upValue , 1 ) ;
322+
323+ } ) ;
324+
325+ it ( "should have the correct value when isPullup is true" , async function ( ) {
326+
327+ const button = await new Button ( {
328+ pin : 13 ,
329+ io : Digital ,
330+ isPullup : true
331+ } ) ;
332+
333+ assert . equal ( button . upValue , 1 ) ;
334+
335+ } ) ;
336+
337+ it ( "should have the correct value when isPullup and invert are true" , async function ( ) {
338+
339+ const button = await new Button ( {
340+ pin : 13 ,
341+ io : Digital ,
342+ isPullup : true ,
343+ invert : true
344+ } ) ;
345+
346+ assert . equal ( button . upValue , 0 ) ;
347+
348+ } ) ;
349+
350+ it ( "should not be settable" , async function ( ) {
351+ const button = await new Button ( {
352+ pin : 13 ,
353+ io : Digital
354+ } ) ;
355+
356+ chai . expect ( ( ) => {
357+ button . upValue = 1 ;
358+ } ) . to . throw ( TypeError ) ;
359+ } ) ;
360+
361+ } ) ;
362+
73363 } ) ;
74364
75365 describe ( "Events" , function ( ) {
@@ -167,5 +457,4 @@ describe("Button", function() {
167457 } ) ;
168458 } ) ;
169459
170-
171460} ) ;
0 commit comments