Socialify

Folder ..

Viewing all_functional.spec.ts
111 lines (97 loc) • 3.0 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
import { describe, expect, it } from 'vitest';
import { faker } from '../dist/cjs';

const IGNORED_MODULES = [
  'locales',
  'locale',
  'localeFallback',
  'definitions',
  'fake',
  'helpers',
  'mersenne',
];

const IGNORED_METHODS = {
  system: ['directoryPath', 'filePath'], // these are TODOs
};

function isTestableModule(mod: string) {
  return IGNORED_MODULES.indexOf(mod) === -1;
}

function isMethodOf(mod: string) {
  return (meth: string) => typeof faker[mod][meth] === 'function';
}

function isTestableMethod(mod: string) {
  return (meth: string) =>
    !(mod in IGNORED_METHODS && IGNORED_METHODS[mod].indexOf(meth) >= 0);
}

function both(
  pred1: (meth: string) => boolean,
  pred2: (meth: string) => boolean
): (meth: string) => boolean {
  return (value) => pred1(value) && pred2(value);
}

// Basic smoke tests to make sure each method is at least implemented and returns a value.

function modulesList(): { [module: string]: string[] } {
  const modules = Object.keys(faker)
    .filter(isTestableModule)
    .reduce((result, mod) => {
      result[mod] = Object.keys(faker[mod]).filter(
        both(isMethodOf(mod), isTestableMethod(mod))
      );
      return result;
    }, {});

  return modules;
}

const modules = modulesList();

describe('functional tests', () => {
  for (const locale in faker.locales) {
    describe(locale, () => {
      faker.locale = locale;
      Object.keys(modules).forEach((module) => {
        describe(module, () => {
          // if there is nothing to test, create a dummy test so the test runner doesn't complain
          if (Object.keys(modules[module]).length === 0) {
            it.todo(`${module} was empty`);
          }

          modules[module].forEach((meth) => {
            it(meth + '()', () => {
              const result = faker[module][meth]();
              if (meth === 'boolean') {
                expect(typeof result).toBe('boolean');
              } else {
                expect(result).toBeTruthy();
              }
            });
          });
        });
      });
    });
  }
});

describe('faker.fake functional tests', () => {
  for (const locale in faker.locales) {
    describe(locale, () => {
      faker.locale = locale;
      faker.seed(1);
      Object.keys(modules).forEach((module) => {
        describe(module, () => {
          // if there is nothing to test, create a dummy test so the test runner doesn't complain
          if (Object.keys(modules[module]).length === 0) {
            it.todo(`${module} was empty`);
          }

          modules[module].forEach((meth) => {
            it(meth + '()', () => {
              const result = faker.fake('{{' + module + '.' + meth + '}}');
              // just make sure any result is returned
              // an undefined result usually means an error
              expect(result).toBeDefined();
              // if (meth === 'boolean') {
              //   expect(typeof result).toBe('boolean');
              // } else {
              //   expect(result).toBeTruthy();
              // }
            });
          });
        });
      });
    });
  }
});