{"size": 1766, "ext": "js", "max_stars_repo_path": "test/unit/renderer/utf8.test.js", "max_stars_repo_name": "tallesnicacio/qrcode-generator", "max_stars_repo_stars_event_min_datetime": "2015-01-04T23:01:09.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T15:02:35.000Z", "max_issues_repo_path": "test/unit/renderer/utf8.test.js", "max_issues_repo_name": "tallesnicacio/qrcode-generator", "max_issues_repo_issues_event_min_datetime": "2015-03-20T16:53:57.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-22T01:42:18.000Z", "max_forks_repo_path": "test/unit/renderer/utf8.test.js", "max_forks_repo_name": "tallesnicacio/qrcode-generator", "max_forks_repo_forks_event_min_datetime": "2015-01-06T22:02:46.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-23T09:24:39.000Z"}
{"max_stars_count": 5384, "max_issues_count": 242, "max_forks_count": 552, "avg_line_length": 25.2285714286, "max_line_length": 69, "alphanum_fraction": 0.6653454134}
const test = require('tap').test
const sinon = require('sinon')
const fs = require('fs')
const QRCode = require('core/qrcode')
const Utf8Renderer = require('renderer/utf8')

test('Utf8Renderer interface', function (t) {
  t.type(Utf8Renderer.render, 'function',
    'Should have render function')

  t.end()
})

test('Utf8Renderer render', function (t) {
  const sampleQrData = QRCode.create('sample text', { version: 2 })
  let str

  t.notThrow(function () { str = Utf8Renderer.render(sampleQrData) },
    'Should not throw with only qrData param')

  t.notThrow(function () {
    str = Utf8Renderer.render(sampleQrData, {
      margin: 10,
      scale: 1
    })
  }, 'Should not throw with options param')

  t.type(str, 'string',
    'Should return a string')

  t.end()
})

test('Utf8 renderToFile', function (t) {
  const sampleQrData = QRCode.create('sample text', { version: 2 })
  const fileName = 'qrimage.txt'
  let fsStub = sinon.stub(fs, 'writeFile').callsArg(2)

  t.plan(5)

  Utf8Renderer.renderToFile(fileName, sampleQrData, function (err) {
    t.ok(!err,
      'Should not generate errors with only qrData param')

    t.equal(fsStub.getCall(0).args[0], fileName,
      'Should save file with correct file name')
  })

  Utf8Renderer.renderToFile(fileName, sampleQrData, {
    margin: 10,
    scale: 1
  }, function (err) {
    t.ok(!err,
      'Should not generate errors with options param')

    t.equal(fsStub.getCall(0).args[0], fileName,
      'Should save file with correct file name')
  })

  fsStub.restore()
  fsStub = sinon.stub(fs, 'writeFile').callsArgWith(2, new Error())

  Utf8Renderer.renderToFile(fileName, sampleQrData, function (err) {
    t.ok(err,
      'Should fail if error occurs during save')
  })

  fsStub.restore()
})
