{"size": 1825, "ext": "js", "max_stars_repo_path": "packages/@hidoo/handlebars-helpers/test/toJson.test.js", "max_stars_repo_name": "hidoo/handlebars-lib", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/@hidoo/handlebars-helpers/test/toJson.test.js", "max_issues_repo_name": "hidoo/handlebars-lib", "max_issues_repo_issues_event_min_datetime": "2019-09-30T07:08:19.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-31T17:53:18.000Z", "max_forks_repo_path": "packages/@hidoo/handlebars-helpers/test/toJson.test.js", "max_forks_repo_name": "hidoo/handlebars-lib", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 13, "max_forks_count": null, "avg_line_length": 28.0769230769, "max_line_length": 96, "alphanum_fraction": 0.5276712329}
/**
 * import modules
 */
import assert from 'assert';
import Handlebars from 'handlebars';
import toJson, {register} from '../src/toJson';

describe('{{toJson value prettify=true}}', () => {
  let template = null;

  before(() => {
    const hbs = Handlebars.create();

    hbs.registerHelper('toJson', toJson);
    template = hbs.compile('{{{toJson value prettify=options.prettify}}}');
  });

  it('should return JSON string if argument "value" is valid.', () => {
    const values = [
      [0, '0'],
      ['value', '"value"'],
      [null, 'null'],
      [[0, 1, 2], '[0,1,2]'],
      [{key1: 'value1', key2: 'value2'}, '{"key1":"value1","key2":"value2"}'],
      [{[{}]: 'value1', [[]]: 'value2'}, '{"[object Object]":"value1","":"value2"}']
    ];

    values.forEach(([value, expected]) => {
      const result = template({value});

      assert(typeof result === 'string');
      assert(result === expected);
    });
  });

  it('should return prettify JSON string if attribute "prettify" is truthy.', () => {
    const values = [
      [0, '0'],
      ['value', '"value"'],
      [null, 'null'],
      [[0, 1, 2], '[\n  0,\n  1,\n  2\n]'],
      [{key1: 'value1', key2: 'value2'}, '{\n  "key1": "value1",\n  "key2": "value2"\n}'],
      [{[{}]: 'value1', [[]]: 'value2'}, '{\n  "[object Object]": "value1",\n  "": "value2"\n}']
    ];

    values.forEach(([value, expected]) => {
      const result = template({value, options: {prettify: true}});

      assert(typeof result === 'string');
      assert(result === expected);
    });
  });

  describe('register', () => {
    it('should be registered.', () => {
      const hbs = Handlebars.create();

      register(hbs);
      hbs.registerHelper('viaRegisterHelper', toJson);

      assert(hbs.helpers.toJson === hbs.helpers.viaRegisterHelper);
    });
  });
});
