|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const moment = require('moment') |
| 4 | +const luxon = require('luxon') |
4 | 5 | const {test, threw} = require('tap') |
5 | 6 | const Chain = require('../../lib/chain.js') |
6 | 7 | const actions = require('../fixtures/actions/index.js') |
@@ -104,11 +105,11 @@ test('Setup chain', async (t) => { |
104 | 105 |
|
105 | 106 | t.match(state, { |
106 | 107 | names: 'Hello, Mr. Wonderful' |
107 | | - }, 'expected output') |
| 108 | + }, 'expected printNames output') |
108 | 109 | } |
109 | 110 | }) |
110 | 111 |
|
111 | | - t.test('extended chain w/ lookup functions', async (t) => { |
| 112 | + t.test('extended chain w/ lookup functions (moment)', async (t) => { |
112 | 113 | class MomentChain extends Chain { |
113 | 114 | constructor(state) { |
114 | 115 | super(state) |
@@ -141,6 +142,40 @@ test('Setup chain', async (t) => { |
141 | 142 | } |
142 | 143 | }) |
143 | 144 |
|
| 145 | + t.test('extended chain w/ lookup functions (luxon)', async (t) => { |
| 146 | + class LuxonChain extends Chain { |
| 147 | + constructor(state) { |
| 148 | + super(state) |
| 149 | + } |
| 150 | + |
| 151 | + $now() { |
| 152 | + return luxon.DateTime.now() |
| 153 | + } |
| 154 | + |
| 155 | + $dateadd(date, amount, units = 'minutes', format) { |
| 156 | + const [_date, _amount, _units] = this.lookup([date, amount, units]) |
| 157 | + let out = _date.isLuxonDateTime ? _date : new luxon.DateTime(_date) |
| 158 | + out = out.plus({[_units]: _amount}) |
| 159 | + return format ? this.$strftime(format, out) : out |
| 160 | + } |
| 161 | + |
| 162 | + $strftime(format = 'Y-MM-DD', date) { |
| 163 | + const input = this.lookup(date) || undefined |
| 164 | + return input.setZone('utc').toFormat(format) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + const chain = new LuxonChain() |
| 169 | + { |
| 170 | + const value = chain.lookup('!dateadd:!now,1,day') |
| 171 | + t.ok(luxon.DateTime.isDateTime(value), 'is luxon DateTime value') |
| 172 | + } |
| 173 | + { |
| 174 | + const value = chain.lookup('!dateadd:!now,1,day,x') |
| 175 | + t.match(value, /\d+/, 'formated number value') |
| 176 | + } |
| 177 | + }) |
| 178 | + |
144 | 179 | t.test('extended chain created with an existing state', async (t) => { |
145 | 180 | class WithStateChain extends Chain { |
146 | 181 | constructor(state) { |
|
0 commit comments