Skip to content

Commit

Permalink
Moar unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed May 9, 2024
1 parent d46b582 commit ece6bba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
21 changes: 5 additions & 16 deletions app/static/app/js/classes/Units.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,57 @@ import { _ } from './gettext';

const units = {
acres: {
factor: 0.00024711,
label: _('Acres'),
factor: 1 / 4046.85642,
abbr: 'ac',
round: 5
},
feet: {
factor: 3.2808,
label: _('Feet'),
factor: 3.28084,
abbr: 'ft',
round: 4
},
hectares: {
factor: 0.0001,
label: _('Hectares'),
abbr: 'ha',
round: 4
},
meters: {
factor: 1,
label: _('Meters'),
abbr: 'm',
round: 3
},
kilometers: {
factor: 0.001,
label: _('Kilometers'),
abbr: 'km',
round: 5
},
centimeters: {
factor: 100,
label: _('Centimeters'),
abbr: 'cm',
round: 1
},
miles: {
factor: 3.2808 / 5280,
label: _('Miles'),
factor: 3.28084 / 5280,
abbr: 'mi',
round: 5
},
sqfeet: {
factor: 10.7639,
label: _('Square Feet'),
factor: 1 / 0.09290304,
abbr: 'ft²',
round: 2
},
sqmeters: {
factor: 1,
label: _('Square Meters'),
abbr: 'm²',
round: 2
},
sqmeters: {
sqkilometers: {
factor: 0.000001,
label: _('Square Kilometers'),
abbr: 'km²',
round: 5
},
sqmiles: {
factor: 0.000000386102,
label: _('Square Miles'),
abbr: 'mi²',
round: 5
}
Expand Down
35 changes: 34 additions & 1 deletion app/static/app/js/components/tests/Units.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,44 @@ describe('Metric system', () => {
[11005, "1.1005 ha"],
[999999, "99.9999 ha"],
[1000000, "1 km²"],
[1000000000, "1,000 km²"]
[1000000000, "1,000 km²"],
[1000255558, "1,000.25556 km²"]
];

areas.forEach(a => {
expect(metric.area(a[0]).toString()).toBe(a[1]);
});
})
});

describe('Imperial system', () => {
it('it should display units properly', () => {

const { imperial } = systems;

const lengths = [
[1, "3.2808 ft"],
[0.01, "0.0328 ft"],
[0.0154, "0.0505 ft"],
[1609, "5,278.8716 ft"],
[1609.344, "1 mi"],
[3218.69, "2 mi"]
];

lengths.forEach(l => {
expect(imperial.length(l[0]).toString()).toBe(l[1]);
});

const areas = [
[1, "10.76 ft²"],
[9999, "2.47081 ac"],
[4046.86, "1 ac"],
[2587398.1, "639.35999 ac"],
[2.59e+6, "1 mi²"]
];

areas.forEach(a => {
expect(imperial.area(a[0]).toString()).toBe(a[1]);
});
})
});

0 comments on commit ece6bba

Please sign in to comment.