Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
frakti committed May 18, 2020
1 parent 98847be commit 1607467
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
6 changes: 3 additions & 3 deletions examples/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const voucherify = voucherifyClient({
clientSecretKey: '3266b9f8-e246-4f79-bdf0-833929b1380c'
})

async function scrollCustomers() {
for await (const customer of voucherify.customers.scroll({limit: 5})) {
console.log("Customer", customer)
async function scrollCustomers () {
for await (const customer of voucherify.customers.scroll({ limit: 5 })) {
console.log('Customer', customer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
},
"scripts": {
"test": "mocha",
"lint": "standard"
"lint": "standard --fix"
}
}
6 changes: 3 additions & 3 deletions src/Customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = class Customers {
async * scroll (params = {}) {
let startingAfter = params.starting_after
let endingBefore = params.ending_before
let direction = (startingAfter || !endingBefore) ? 'desc' : 'asc'
const direction = (startingAfter || !endingBefore) ? 'desc' : 'asc'

let response = await this.client.get('/customers', Object.assign(
{}, params, {
Expand All @@ -39,7 +39,7 @@ module.exports = class Customers {

while (true) {
if (response.customers.length === 0) {
break;
break
}
for (const customer of response.customers) {
// comparing ISOs
Expand All @@ -51,7 +51,7 @@ module.exports = class Customers {
yield customer
}
if (!response.has_more) {
break;
break
}

response = await this.client.get(
Expand Down
48 changes: 30 additions & 18 deletions test/customers-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,89 +82,101 @@ describe('Customers API', function () {
})
})


describe('scroll', async function () {
it('should scroll customers descending', async function () {
var server = nock('https://api.voucherify.io', reqWithoutBody)
.get('/v1/customers')
.query({ filters: 'value', starting_after: '1970-01-01T00:00:00Z' })
.reply(200, {has_more: true, customers: [{created_at: '2020-01-01T00:00:00Z'}, {created_at: '2020-01-02T00:00:00Z'}]})
.reply(200, { has_more: true, customers: [{ created_at: '2020-01-01T00:00:00Z' }, { created_at: '2020-01-02T00:00:00Z' }] })

.get('/v1/customers')
.query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z' })
.reply(200, {has_more: false, customers: [{created_at: '2020-01-03T00:00:00Z'}]})

.reply(200, { has_more: false, customers: [{ created_at: '2020-01-03T00:00:00Z' }] })

let callCount = 0
const customers = []
for await (const customer of client.customers.scroll({ filters: 'value' })) {
++callCount
customers.push(customer)
}

expect(callCount).to.equal(3)
expect(customers[0]).to.eql({ created_at: '2020-01-01T00:00:00Z' })
await server.done()
})

it('should scroll customers descending and defined initial starting_after', async function () {
var server = nock('https://api.voucherify.io', reqWithoutBody)
.get('/v1/customers')
.query({ filters: 'value', starting_after: '2019-12-31T23:59:00Z' })
.reply(200, {has_more: true, customers: [{created_at: '2020-01-01T00:00:00Z'}, {created_at: '2020-01-02T00:00:00Z'}]})
.reply(200, { has_more: true, customers: [{ created_at: '2020-01-01T00:00:00Z' }, { created_at: '2020-01-02T00:00:00Z' }] })

.get('/v1/customers')
.query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z' })
.reply(200, {has_more: false, customers: [{created_at: '2020-01-03T00:00:00Z'}]})
.reply(200, { has_more: false, customers: [{ created_at: '2020-01-03T00:00:00Z' }] })

let callCount = 0
const customers = []
for await (const customer of client.customers.scroll({ starting_after: '2019-12-31T23:59:00Z', filters: 'value' })) {
++callCount
customers.push(customer)
}
expect(callCount).to.equal(3)
expect(customers[0]).to.eql({ created_at: '2020-01-01T00:00:00Z' })
await server.done()
})

it('should scroll customers descending and defined initial starting_after and ending_before', async function () {
var server = nock('https://api.voucherify.io', reqWithoutBody)
.get('/v1/customers')
.query({ filters: 'value', starting_after: '2019-12-31T23:59:00Z', ending_before: '2020-12-31T23:59:00Z' })
.reply(200, {has_more: true, customers: [{created_at: '2020-01-01T00:00:00Z'}, {created_at: '2020-01-02T00:00:00Z'}]})
.reply(200, { has_more: true, customers: [{ created_at: '2020-01-01T00:00:00Z' }, { created_at: '2020-01-02T00:00:00Z' }] })

.get('/v1/customers')
.query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z', ending_before : '2020-12-31T23:59:00Z' })
.reply(200, {has_more: false, customers: [{created_at: '2020-01-03T00:00:00Z'}]})
.query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z', ending_before: '2020-12-31T23:59:00Z' })
.reply(200, { has_more: false, customers: [{ created_at: '2020-01-03T00:00:00Z' }] })

let callCount = 0
const customers = []
for await (const customer of client.customers.scroll({
starting_after: '2019-12-31T23:59:00Z',
ending_before : '2020-12-31T23:59:00Z',
ending_before: '2020-12-31T23:59:00Z',
filters: 'value'
})) {
++callCount
customers.push(customer)
}
expect(callCount).to.equal(3)
expect(customers[0]).to.eql({ created_at: '2020-01-01T00:00:00Z' })
await server.done()
})

it('should scroll customers ascending', async function () {
let now = new Date().toISOString()
const now = new Date().toISOString()
var server = nock('https://api.voucherify.io', reqWithoutBody)
.get('/v1/customers')
.query({ ending_before: now, filters: 'value' })
.reply(200, {has_more: true, customers: [
{created_at: '2020-01-04T00:00:00Z'},
{created_at: '2020-01-03T00:00:00Z'},
{created_at: '2020-01-02T00:00:00Z'}
]})
.reply(200, {
has_more: true,
customers: [
{ created_at: '2020-01-04T00:00:00Z' },
{ created_at: '2020-01-03T00:00:00Z' },
{ created_at: '2020-01-02T00:00:00Z' }
]
})
.get('/v1/customers')
.query({ filters: 'value', ending_before: '2020-01-02T00:00:00Z' })
.reply(200, {has_more: false, customers: [{created_at: '2020-01-01T00:00:00Z'}]})

.reply(200, { has_more: false, customers: [{ created_at: '2020-01-01T00:00:00Z' }] })

let callCount = 0
const customers = []
for await (const customer of client.customers.scroll({ filters: 'value', ending_before: now })) {
++callCount
customers.push(customer)
}

expect(callCount).to.equal(4)
expect(customers[0]).to.eql({ created_at: '2020-01-04T00:00:00Z' })
await server.done()
})
})
Expand Down

0 comments on commit 1607467

Please sign in to comment.