Skip to content

Commit

Permalink
chore: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Feb 29, 2024
1 parent 8357168 commit baf71df
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 51 deletions.
45 changes: 18 additions & 27 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,40 +318,31 @@ export class CommerceModule extends ModuleBase {
return `${symbol}${min.toFixed(dec)}`;
}

let generated: number;

try {
generated = this.faker.number.float({
min,
max,
precision: (1 / 10) ** (dec - 1),
});
} catch {
generated = this.faker.number.float({
min,
max,
precision: (1 / 10) ** dec,
});
}
const generated = this.faker.number.float({
min,
max,
fractionDigits: dec,
});

if (dec === 0) {
return `${symbol}${generated.toFixed(dec)}`;
}

const oldLastDigit = (generated * 10 ** dec) % 10;
const newLastDigit =
(1 / 10) ** dec *
this.faker.helpers.weightedArrayElement([
{ weight: 5, value: 9 },
{ weight: 3, value: 5 },
{ weight: 1, value: 0 },
{
weight: 1,
value: this.faker.number.int({ min: 0, max: 9 }),
},
]);
const newLastDigit = this.faker.helpers.weightedArrayElement([
{ weight: 5, value: 9 },
{ weight: 3, value: 5 },
{ weight: 1, value: 0 },
{
weight: 1,
value: this.faker.number.int({ min: 0, max: 9 }),
},
]);

const combined = generated - oldLastDigit + newLastDigit;
const fraction = (1 / 10) ** dec;
const oldLastDigitValue = oldLastDigit * fraction;
const newLastDigitValue = newLastDigit * fraction;
const combined = generated - oldLastDigitValue + newLastDigitValue;

if (combined <= max && combined >= min) {
return `${symbol}${combined.toFixed(dec)}`;
Expand Down
48 changes: 24 additions & 24 deletions test/modules/__snapshots__/commerce.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ exports[`commerce > 42 > isbn > with variant 10 and space separators 1`] = `"0 9

exports[`commerce > 42 > isbn > with variant 13 1`] = `"978-0-9751108-6-7"`;

exports[`commerce > 42 > price > noArgs 1`] = `"375.27"`;
exports[`commerce > 42 > price > noArgs 1`] = `"375.15"`;

exports[`commerce > 42 > price > with float min and float max option 1`] = `"1.07"`;
exports[`commerce > 42 > price > with float min and float max option 1`] = `"1.05"`;

exports[`commerce > 42 > price > with max 1`] = `"375.27"`;
exports[`commerce > 42 > price > with max 1`] = `"375.15"`;

exports[`commerce > 42 > price > with max option 1`] = `"501.47"`;
exports[`commerce > 42 > price > with max option 1`] = `"501.35"`;

exports[`commerce > 42 > price > with min 1`] = `"405.87"`;
exports[`commerce > 42 > price > with min 1`] = `"405.85"`;

exports[`commerce > 42 > price > with min and max 1`] = `"68.77"`;
exports[`commerce > 42 > price > with min and max 1`] = `"68.75"`;

exports[`commerce > 42 > price > with min and max and decimals 1`] = `"68.7277"`;
exports[`commerce > 42 > price > with min and max and decimals 1`] = `"68.7275"`;

exports[`commerce > 42 > price > with min and max and decimals and symbol 1`] = `"$68.7277"`;
exports[`commerce > 42 > price > with min and max and decimals and symbol 1`] = `"$68.7275"`;

exports[`commerce > 42 > price > with min and max and decimals and symbol option 1`] = `"$68.7277"`;
exports[`commerce > 42 > price > with min and max and decimals and symbol option 1`] = `"$68.7275"`;

exports[`commerce > 42 > price > with min and max and decimals option 1`] = `"68.7277"`;
exports[`commerce > 42 > price > with min and max and decimals option 1`] = `"68.7275"`;

exports[`commerce > 42 > price > with min and max option 1`] = `"68.77"`;
exports[`commerce > 42 > price > with min and max option 1`] = `"68.75"`;

exports[`commerce > 42 > price > with min option 1`] = `"400.87"`;
exports[`commerce > 42 > price > with min option 1`] = `"400.85"`;

exports[`commerce > 42 > product 1`] = `"Pants"`;

Expand All @@ -58,29 +58,29 @@ exports[`commerce > 1211 > isbn > with variant 10 and space separators 1`] = `"1

exports[`commerce > 1211 > isbn > with variant 13 1`] = `"978-1-82966-736-0"`;

exports[`commerce > 1211 > price > noArgs 1`] = `"928.60"`;
exports[`commerce > 1211 > price > noArgs 1`] = `"928.69"`;

exports[`commerce > 1211 > price > with float min and float max option 1`] = `"1.10"`;

exports[`commerce > 1211 > price > with max 1`] = `"928.60"`;
exports[`commerce > 1211 > price > with max 1`] = `"928.69"`;

exports[`commerce > 1211 > price > with max option 1`] = `"1241.50"`;
exports[`commerce > 1211 > price > with max option 1`] = `"1241.59"`;

exports[`commerce > 1211 > price > with min 1`] = `"932.10"`;
exports[`commerce > 1211 > price > with min 1`] = `"932.19"`;

exports[`commerce > 1211 > price > with min and max 1`] = `"96.50"`;
exports[`commerce > 1211 > price > with min and max 1`] = `"96.49"`;

exports[`commerce > 1211 > price > with min and max and decimals 1`] = `"96.4260"`;
exports[`commerce > 1211 > price > with min and max and decimals 1`] = `"96.4269"`;

exports[`commerce > 1211 > price > with min and max and decimals and symbol 1`] = `"$96.4260"`;
exports[`commerce > 1211 > price > with min and max and decimals and symbol 1`] = `"$96.4269"`;

exports[`commerce > 1211 > price > with min and max and decimals and symbol option 1`] = `"$96.4260"`;
exports[`commerce > 1211 > price > with min and max and decimals and symbol option 1`] = `"$96.4269"`;

exports[`commerce > 1211 > price > with min and max and decimals option 1`] = `"96.4260"`;
exports[`commerce > 1211 > price > with min and max and decimals option 1`] = `"96.4269"`;

exports[`commerce > 1211 > price > with min and max option 1`] = `"96.50"`;
exports[`commerce > 1211 > price > with min and max option 1`] = `"96.49"`;

exports[`commerce > 1211 > price > with min option 1`] = `"931.60"`;
exports[`commerce > 1211 > price > with min option 1`] = `"931.59"`;

exports[`commerce > 1211 > product 1`] = `"Sausages"`;

Expand Down Expand Up @@ -112,7 +112,7 @@ exports[`commerce > 1337 > price > with max 1`] = `"262.79"`;

exports[`commerce > 1337 > price > with max option 1`] = `"351.09"`;

exports[`commerce > 1337 > price > with min 1`] = `"288.99"`;
exports[`commerce > 1337 > price > with min 1`] = `"298.99"`;

exports[`commerce > 1337 > price > with min and max 1`] = `"63.19"`;

Expand Down

0 comments on commit baf71df

Please sign in to comment.