Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit 752c8c2

Browse files
committed
✨ (createCanal) Introduce createCanal function
1 parent c18b5af commit 752c8c2

10 files changed

Lines changed: 70 additions & 20 deletions

File tree

example/src/canals/SignIn/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { canal } from 'react-gondola';
1+
import { createCanal } from 'react-gondola';
22
import { FirstName } from './FirstName';
33
import { LastName } from './LastName';
44

55
// @ts-ignore
6-
export const SignIn = canal(FirstName, LastName);
6+
export const SignIn = createCanal(FirstName, LastName);

src/Navigation.store.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { observable } from 'mobx';
2+
import { Canal } from 'Canal';
3+
4+
interface INavigationState {}
5+
interface ICanalsMap {
6+
[key: string]: Canal;
7+
}
8+
9+
export class Navigation {
10+
@observable
11+
state: INavigationState = {};
12+
canalsMap: ICanalsMap = {};
13+
}

src/__tests__/Canal.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Canal } from '../Canal';
2+
3+
describe('Canal', () => {
4+
it('initializes with a unique id', () => {
5+
const canal = new Canal();
6+
expect(canal.id).toEqual(0);
7+
});
8+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Navigation } from '../Navigation.store';
2+
3+
describe('Navigation store', () => {
4+
it('initializes with an empty state and canalsMap', () => {
5+
const navigation = new Navigation();
6+
expect(navigation.state).toEqual({});
7+
expect(navigation.canalsMap).toEqual({});
8+
});
9+
10+
describe('state', () => {
11+
it('cannot be changed directly', () => {
12+
try {
13+
const navigation = new Navigation();
14+
navigation.state = {};
15+
} catch (error) {
16+
expect(error.message).toMatch(/\[mobx\]/);
17+
}
18+
expect.assertions(1);
19+
});
20+
});
21+
});

src/__tests__/__snapshots__/canal.test.tsx.snap

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`createCanal renders the first page when mounted 1`] = `<View />`;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import React from 'react';
22
import { View } from 'react-native';
33
import TestRenderer from 'react-test-renderer';
44

5-
import { canal } from '../canal';
5+
import { createCanal } from '../createCanal';
66

7-
describe('canal', () => {
7+
describe('createCanal', () => {
88
it('throws an error if first arg is not a Component', () => {
99
try {
10-
canal('aaa');
10+
createCanal('aaa');
1111
} catch (error) {
1212
expect(error.message).toBe(
13-
'`canal` expects its first argument to be a React component. Received type: string'
13+
'`createCanal` expects its first argument to be a React component. Received type: string'
1414
);
1515
}
1616
expect.assertions(1);
1717
});
1818

1919
it('renders the first page when mounted', () => {
20-
const Canal = canal(View);
20+
const Canal = createCanal(View);
2121
const testRenderer = TestRenderer.create(<Canal />);
2222
expect(testRenderer.toJSON()).toMatchSnapshot();
2323
});

src/canal.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import React, { ComponentType, FunctionComponent } from 'react';
1+
export interface IStop {
2+
name: string;
3+
}
24

3-
export const canal = (Page: ComponentType): FunctionComponent => {
4-
if (!(React.isValidElement(Page) || typeof Page === 'function')) {
5-
throw new Error(
6-
`\`canal\` expects its first argument to be a React component. Received type: ${typeof Page}`
7-
);
8-
}
9-
return () => <Page />;
10-
};
5+
export class Canal {
6+
id: number = Date.now();
7+
stopsList: IStop[] = [];
8+
}

src/createCanal.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React, { ComponentType, FunctionComponent } from 'react';
2+
3+
export const createCanal = (Page: ComponentType): FunctionComponent => {
4+
if (!(React.isValidElement(Page) || typeof Page === 'function')) {
5+
throw new Error(
6+
`\`createCanal\` expects its first argument to be a React component. Received type: ${typeof Page}`
7+
);
8+
}
9+
return () => <Page />;
10+
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { canal } from './canal';
1+
export { createCanal } from './createCanal';
22
export { NavigationProvider } from './NavigationProvider';

0 commit comments

Comments
 (0)