text: /* eslint-env mocha */
  const assert = require('assert')
  const TestHelper = require('../../../../test-helper.js')
  const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
  const ScreenshotData = require('../../../../screenshot-data.js')
  
  describe('/administrator/subscriptions/products', function () {
    let cachedResponses, cachedProducts
    async function bundledData (retryNumber) {
      if (retryNumber > 0) {
        cachedResponses = {}
        await TestHelper.rotateWebhook(true)
      }
      if (cachedResponses && cachedResponses.finished) {
        return
      }
      cachedResponses = {}
      cachedProducts = []
      await DashboardTestHelper.setupBeforeEach()
      await TestHelper.setupBeforeEach()
      const administrator = await TestHelper.createOwner()
      for (let i = 0, len = global.pageSize + 2; i < len; i++) {
        await TestHelper.createProduct(administrator)
        cachedProducts.unshift(administrator.product.productid)
      }
      const req1 = TestHelper.createRequest('/administrator/subscriptions/products')
      req1.account = administrator.account
      req1.session = administrator.session
      req1.filename = __filename
      req1.screenshots = [
        { hover: '#administrator-menu-container' },
        { click: '/administrator/subscriptions' },
        { click: '/administrator/subscriptions/products' }
      ]
      await req1.route.api.before(req1)
      cachedResponses.before = req1.data
      cachedResponses.returns = await req1.get()
      global.pageSize = 50
      global.packageJSON.dashboard.server.push(ScreenshotData.administratorIndex)
      global.packageJSON.dashboard.server.push(ScreenshotData.administratorProducts)
      global.pageSize = 3
      delete (req1.screenshots)
      cachedResponses.pageSize = await req1.get()
      global.pageSize = 2
      const req2 = TestHelper.createRequest('/administrator/subscriptions/products?offset=1')
      req2.account = administrator.account
      req2.session = administrator.session
      cachedResponses.offset = await req2.get()
      cachedResponses.finished = true
    }
    describe('before', () => {
      it('should bind data to req', async function () {
        await bundledData(this.test.currentRetry())
        const data = cachedResponses.before
        assert.strictEqual(data.products.length, global.pageSize)
        assert.strictEqual(data.products[0].id, cachedProducts[0])
        assert.strictEqual(data.products[1].id, cachedProducts[1])
      })
    })
  
    describe('view', () => {
      it('should return one page (screenshots)', async function () {
        await bundledData(this.test.currentRetry())
        const result = cachedResponses.returns
        const doc = TestHelper.extractDoc(result.html)
        const table = doc.getElementById('products-table')
        const rows = table.getElementsByTagName('tr')
        assert.strictEqual(rows.length, global.pageSize + 1)
      })
  
      it('should change page size', async function () {
        await bundledData(this.test.currentRetry())
        const result = cachedResponses.pageSize
        const doc = TestHelper.extractDoc(result.html)
        const table = doc.getElementById('products-table')
        const rows = table.getElementsByTagName('tr')
        assert.strictEqual(rows.length, global.pageSize + 2)
      })
  
      it('should change offset', async function () {
        await bundledData(this.test.currentRetry())
        const offset = 1
        const result = cachedResponses.offset
        const doc = TestHelper.extractDoc(result.html)
        for (let i = 0, len = global.pageSize; i < len; i++) {
          assert.strictEqual(doc.getElementById(cachedProducts[offset + i]).tag, 'tr')
        }
      })
    })
  })
  