Skip to content

Commit

Permalink
fix: pickMatch return value based on generic
Browse files Browse the repository at this point in the history
  • Loading branch information
thearnica committed Mar 13, 2019
1 parent 90441ed commit b2db650
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
50 changes: 46 additions & 4 deletions __tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,49 @@ describe('Specs', () => {
});

it('pickMatch', () => {
expect(pickMatch({
mobile: false,
tablet: false,
desktop: false,
}, {
tablet: 2,
})).toBe(2);

expect(pickMatch({
mobile: false,
tablet: false,
desktop: false,
}, {
})).toBe(undefined);

expect(pickMatch({
mobile: false,
tablet: false,
desktop: false,
}, {
desktop: 3,
})).toBe(3);

expect(pickMatch({
mobile: false,
tablet: false,
desktop: false,
}, {
mobile: 1,
tablet: 2,
desktop: 3,
})).toBe(3);

expect(pickMatch({
mobile: true,
tablet: true,
desktop: true,
}, {
mobile: 1,
tablet: 2,
desktop: 3,
})).toBe(1);

expect(pickMatch({
mobile: false,
tablet: true,
Expand Down Expand Up @@ -156,7 +199,6 @@ describe('Specs', () => {
})).toBe(1);
})


describe('SSR', () => {
it('Render', () => {
const wrapper =
Expand All @@ -173,9 +215,9 @@ describe('Specs', () => {
const wrapper =
create(
<MediaMock tablet>
<MediaServerRender predicted="tablet">
<MediaMatcher mobile="1" tablet="2" desktop="3"/>
</MediaServerRender>
<MediaServerRender predicted="tablet">
<MediaMatcher mobile="1" tablet="2" desktop="3"/>
</MediaServerRender>
</MediaMock>
);
expect(wrapper.toJSON()).toEqual("2");
Expand Down
6 changes: 3 additions & 3 deletions src/createMediaMatcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const castPointsTo = (points: { [key: string]: any }, targetType: any) => (
export type NoChildren = { children?: never };

export type MediaMatcherType<T> = {
pickMatch<K>(matches: BoolOf<T>, slots: Partial<ObjectOf<T, K>>): React.ReactNode | null,
pickMatch<K>(matches: BoolOf<T>, slots: Partial<ObjectOf<T, K>>): K,

Provider: React.SFC<{ state?: MediaRulesOf<T>, override?: false }>;
Mock: React.SFC<Partial<RenderOf<T>>>;
Expand All @@ -40,11 +40,11 @@ export type MediaMatcherType<T> = {
export function createMediaMatcher<T>(breakPoints: MediaRulesOf<T>): MediaMatcherType<T> {
const MediaContext = React.createContext<BoolHash>({});

function pickMatch<K>(matches: BoolOf<T>, slots: Partial<ObjectOf<T, K>>): K | null {
function pickMatch<K>(matches: BoolOf<T>, slots: Partial<ObjectOf<T, K>>): K {
return pickMediaMatch<T, K>(breakPoints, matches, slots)
}

function pickMatchEx<M extends Partial<ObjectOf<T, React.ReactNode>>>(matches: BoolOf<T>, slots: M): React.ReactNode | null {
function pickMatchEx<M extends Partial<ObjectOf<T, React.ReactNode>>>(matches: BoolOf<T>, slots: M): React.ReactNode {
return pickMediaMatch<T, React.ReactNode>(breakPoints, matches, slots)
}

Expand Down
7 changes: 4 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getMaxMatch<T>(mediaRules: MediaRulesOf<T>, matches: BoolOf<any>
}

export function pickMediaMatch<T, K>
(mediaRules: MediaRulesOf<T>, matches: BoolOf<any>, slots: Partial<ObjectOf<any, K>>): K | null {
(mediaRules: MediaRulesOf<T>, matches: BoolOf<any>, slots: Partial<ObjectOf<any, K>>): K {
const keys = Object.keys(mediaRules);
const len = keys.length;

Expand All @@ -44,7 +44,8 @@ export function pickMediaMatch<T, K>
}
}

return null;
// could be only possible if no slots is given, so K is undefined
return undefined as any;
}

export type Names = {
Expand Down Expand Up @@ -88,4 +89,4 @@ export function notNulls(matches: { [key: string]: boolean | undefined }): { [ke

return acc;
}, {})
}
}

0 comments on commit b2db650

Please sign in to comment.