Skip to content

Commit

Permalink
test(design): add tests for daff-map-get errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gracetxgao committed Dec 11, 2024
1 parent 2628e1a commit 8b04521
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
13 changes: 13 additions & 0 deletions libs/design/scss/core/map/map-get/map-get-error.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$debug: false !default;

@function map-get-error($message, $override: $debug) {
@if $override {
@return $message;
} @else {
@error $message;
}
}

@mixin set-debug($state: true) {
$debug: $state !global;
}
3 changes: 2 additions & 1 deletion libs/design/scss/core/map/map-get/map-get.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@use 'sass:map';
@use './map-get-error';

//
// Return a the value of a nested key in a map if it exists.
Expand All @@ -13,7 +14,7 @@
@function daff-map-get($map, $keys...) {
@each $key in $keys {
@if not map.has-key($map, $key) {
@error 'The map doesn\'t contain the $key: `#{$key}`\'';
@return map-get-error.map-get-error("The map doesn't contain the $key: `#{$key}`'");
}
$map: map.get($map, $key);
}
Expand Down
35 changes: 35 additions & 0 deletions libs/design/scss/core/map/map-get/map-get.spec.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@use 'true' as *;
@use './map-get' as *;
@use './map-get-error';

@include describe('map-get') {
$nested-map: (
Expand All @@ -15,11 +16,45 @@
)
);

$deep-map: (
'level1': (
'level2': (
'level3': (
'level': 4
)
)
)
);

$empty-map: ();

@include map-get-error.set-debug(true);

@include it('returns the value of the nested key if it exists in the map') {
@include assert-equal(daff-map-get($map, 'nested', 'string'), '1');
@include assert-equal(daff-map-get($map, 'nested', 'number'), 2);
@include assert-equal(daff-map-get($map, 'nested', 'map'), $nested-map);
@include assert-equal(daff-map-get($map, 'nested', 'map', 'string'), '3');
@include assert-equal(daff-map-get($map, 'nested', 'map', 'number'), 4);
}

@include it('returns the correct value for deeply nested maps') {
@include assert-equal(daff-map-get($deep-map, 'level1', 'level2', 'level3', 'level'), 4);
}

@include it('errors if the value of the nested key does not exist in the map') {
@include assert-equal(
daff-map-get($map, 'missing'),
"The map doesn't contain the $key: `missing`'"
);
}

@include it('errors if the map is empty') {
@include assert-equal(
daff-map-get($empty-map, 'nested'),
"The map doesn't contain the $key: `nested`'"
);
}

@include map-get-error.set-debug(false);
}

0 comments on commit 8b04521

Please sign in to comment.