Skip to content

feat: adds gap property #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Flex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface FlexProps {
justify?: Justify;
direction?: Direction;
reverse?: boolean;
gap?: string;
}

export default class Flex extends SvelteComponentTyped<FlexProps> {}
3 changes: 3 additions & 0 deletions src/Flex.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export let align = 'center';
export let justify = 'center';
export let reverse = false;
export let gap = undefined;

// 'start' | 'center' | 'end' | 'stretch'
const alignMap = {
Expand All @@ -22,6 +23,7 @@
evenly: 'space-evenly',
};


$: directionWithReverse = reverse ? `${direction}-reverse` : direction;
</script>

Expand All @@ -32,6 +34,7 @@
style:flex-direction={directionWithReverse}
style:align-items={alignMap[align]}
style:justify-content={justifyMap[justify]}
style:gap={gap}
>
<slot />
</div>
14 changes: 14 additions & 0 deletions src/__tests__/Flex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,18 @@ describe('<Flex /> component', () => {
expect(container).not.toHaveStyle('flex-direction: column'); // default/fallback
});
});

describe('gap prop', () => {
test('has gap prop set to 1 rem', () => {
const container = renderFlex({ gap: '1rem' });
expect(container).toHaveStyle('gap: 1rem');
expect(container).not.toHaveStyle('gap: 0rem');
});

test('bad value', () => {
const container = renderFlex({ gap: 'oops' });
expect(container).not.toHaveStyle('gap: 1rem');
expect(container).not.toHaveStyle('gap: 0');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests! 😄

Perhaps we can also add an expected fallback/default value? (the initial value we discussed above).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can i test this? as far as i know, if i make the default value for the gap prop equal to undefined, the svelte style directive would just ignore it and not pass anything.
should i expect(container).not.toHaveStyle('gap: 0')?

Copy link
Owner

@himynameisdave himynameisdave Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the initial value for gap is 'normal normal', I think it would be a great idea to do a positive check for that value (I think that testing-library's toHaveStyle should return true because it is using getComputedStyle under the hood, which always returns whatever the initial value is if none is set.

Try this:

expect(container).toHaveStyle({ gap: 'normal normal' });

If not, I'd say the tests you've already added are sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried expecting it to have gap: 'normal normal' but it didnt work. im going to remove the test for "does not has gap prop", but feel free to add it after merging the PR. also, pls check my comment on #30

});
})
});