Skip to content
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

fix(withCamelCaseProps): make exception for className #1534

Merged
merged 1 commit into from
Aug 27, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ exports[`Dialog component snapshot should match component snapshot 1`] = `
bar_content={null}
class={null}
className={null}
class_name={null}
close_button_attributes={null}
close_modal={null}
close_title="Lukk"
Expand Down Expand Up @@ -707,7 +706,6 @@ exports[`Dialog component snapshot should match component snapshot 1`] = `
bypass_invalidation_selectors={null}
class={null}
className={null}
class_name={null}
close={[Function]}
close_button_attributes={null}
close_modal={null}
Expand Down Expand Up @@ -763,7 +761,6 @@ exports[`Dialog component snapshot should match component snapshot 1`] = `
bypass_invalidation_selectors={null}
class={null}
className={null}
class_name={null}
close={[Function]}
close_button_attributes={null}
close_modal={null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ exports[`Drawer component snapshot should match component snapshot 1`] = `
bar_content={null}
class={null}
className={null}
class_name={null}
close_button_attributes={null}
close_modal={null}
close_title="Lukk"
Expand Down Expand Up @@ -705,7 +704,6 @@ exports[`Drawer component snapshot should match component snapshot 1`] = `
bypass_invalidation_selectors={null}
class={null}
className={null}
class_name={null}
close={[Function]}
close_button_attributes={null}
close_modal={null}
Expand Down Expand Up @@ -760,7 +758,6 @@ exports[`Drawer component snapshot should match component snapshot 1`] = `
bypass_invalidation_selectors={null}
class={null}
className={null}
class_name={null}
close={[Function]}
close_button_attributes={null}
close_modal={null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ exports[`Modal component have to match snapshot 1`] = `
bar_content={null}
class={null}
className={null}
class_name={null}
close_button_attributes={null}
close_modal={null}
close_title="Lukk"
Expand Down Expand Up @@ -702,7 +701,6 @@ exports[`Modal component have to match snapshot 1`] = `
bypass_invalidation_selectors={null}
class={null}
className={null}
class_name={null}
close={[Function]}
close_button_attributes={null}
close_modal={null}
Expand Down Expand Up @@ -749,7 +747,6 @@ exports[`Modal component have to match snapshot 1`] = `
bypass_invalidation_selectors={null}
class={null}
className={null}
class_name={null}
close={[Function]}
close_button_attributes={null}
close_modal={null}
Expand Down Expand Up @@ -804,7 +801,6 @@ exports[`Modal component have to match snapshot 1`] = `
bypass_invalidation_selectors={null}
class={null}
className={null}
class_name={null}
close_modal={null}
containerPlacement={null}
focus_selector={null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,6 @@ Array [
bar_content={null}
class={null}
className={null}
class_name={null}
close_button_attributes={null}
close_modal={null}
close_title="Lukk"
Expand Down Expand Up @@ -5361,7 +5360,6 @@ Array [
bar_content={null}
class={null}
className={null}
class_name={null}
close_button_attributes={null}
close_modal={null}
close_title="Lukk"
Expand Down Expand Up @@ -8069,7 +8067,6 @@ Array [
bar_content={null}
class={null}
className={null}
class_name={null}
close_button_attributes={null}
close_modal={null}
close_title="Lukk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,41 @@ describe('withCamelCaseProps', () => {
`)
})

it('will handle className correctly', () => {
type ExtraTypes = {
className?: string
}
const Component = withCamelCaseProps(
(props: IncludeCamelCase<OriginalProps> & ExtraTypes) => {
return <Original {...props} />
}
)

const { asFragment } = render(<Component className="value" />)

expect(screen.queryByTestId('props').textContent).toMatch(
'{"className":"value"}'
)
expect(asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
<div
data-testid="content"
>
<div
data-testid="props"
>
{"className":"value"}
</div>
<div
data-testid="context"
>
{}
</div>
</div>
</DocumentFragment>
`)
})

it('will render with enzyme', () => {
const Comp = mount(<Component snake_case={false} camelCase={1} />)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/react'
import React from 'react'
import {
withSnakeCaseProps,
convertSnakeCaseProps,
classWithSnakeCaseProps,
IncludeSnakeCase,
} from '../withSnakeCaseProps'
Expand Down Expand Up @@ -423,3 +424,18 @@ describe('classWithSnakeCaseProps', () => {
expect(onUpdate).toHaveBeenCalledTimes(2)
})
})

describe('convertSnakeCaseProps', () => {
it('will convert', () => {
const props = {
foo_bar: 'value',
snake_case: { foo_bar: 123 },
}
const result = convertSnakeCaseProps(props)

expect(result).toEqual({
fooBar: 'value',
snakeCase: { foo_bar: 123 },
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ function convertCamelCaseProps<P>(props: P) {
const newProps = { ...props }

for (const key in props) {
switch (key) {
case 'className': {
continue
}
}

if (/^[a-z]+[A-Z]/.test(key)) {
newProps[toSnakeCase(key)] = props[key]
delete newProps[key]
Expand Down