Socialify

Folder ..

Viewing animal.spec.ts
116 lines (108 loc) • 2.7 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
112
113
114
115
116
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../dist/cjs';

const seededRuns = [
  {
    seed: 42,
    expectations: {
      bear: 'Sun bear',
      bird: 'Iceland Gull',
      cat: 'Himalayan',
      cetacean: 'Pantropical Spotted Dolphin',
      cow: 'Fleckvieh',
      crocodilia: 'African Slender-snouted Crocodile',
      dog: 'Garafian Shepherd',
      fish: 'Northern snakehead',
      horse: 'Furioso-North Star',
      insect: 'Gouty oak gall',
      lion: 'West African Lion',
      rabbit: 'English Spot',
      snake: 'Grey-banded kingsnake',
      type: 'lion',
    },
  },
  {
    seed: 1337,
    expectations: {
      bear: 'Sun bear',
      bird: 'American Golden-Plover',
      cat: 'Devon Rex',
      cetacean: 'Costero',
      cow: 'Canchim',
      crocodilia: 'Cuvier’s Dwarf Caiman',
      dog: 'Chinese Crested Dog',
      fish: 'Jumbo flying squid',
      horse: 'Colorado Ranger',
      insect: 'Eulophid wasp',
      lion: 'Barbary Lion',
      rabbit: 'Cinnamon',
      snake: 'Fierce snake',
      type: 'bear',
    },
  },
  {
    seed: 1211,
    expectations: {
      bear: 'Polar bear',
      bird: 'Reed Bunting',
      cat: 'Tonkinese',
      cetacean: 'La Plata Dolphin',
      cow: 'Breed',
      crocodilia: 'Gharial',
      dog: 'Tibetan Spaniel',
      fish: 'Bigeye scad',
      horse: 'Ukrainian Riding Horse',
      insect: 'Western paper wasp',
      lion: 'Cape lion',
      rabbit: 'Silver Marten',
      snake: 'Tiger pit viper',
      type: 'horse',
    },
  },
];

const NON_SEEDED_BASED_RUN = 5;

const functionNames = [
  'bear',
  'bird',
  'cat',
  'cetacean',
  'cow',
  'crocodilia',
  'dog',
  'fish',
  'horse',
  'insect',
  'lion',
  'rabbit',
  'snake',
  'type',
];

describe('animal', () => {
  afterEach(() => {
    faker.locale = 'en';
  });

  for (const { seed, expectations } of seededRuns) {
    describe(`seed: ${seed}`, () => {
      for (const functionName of functionNames) {
        it(`${functionName}()`, () => {
          faker.seed(seed);

          const actual = faker.animal[functionName]();
          expect(actual).toEqual(expectations[functionName]);
        });
      }
    });
  }

  // Create and log-back the seed for debug purposes
  faker.seed(Math.ceil(Math.random() * 1_000_000_000));

  describe(`random seeded tests for seed ${faker.seedValue}`, () => {
    for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
      for (const functionName of functionNames) {
        describe(`${functionName}()`, () => {
          it(`should return random value from ${functionName} array`, () => {
            const actual = faker.animal[functionName]();
            expect(faker.definitions.animal[functionName]).toContain(actual);
          });
        });
      }
    }
  });
});