Skip to content

Commit 279a126

Browse files
committed
Added register & unregister methods to Barricade
Added logic to be able to disable individual APIs in Barricade
1 parent a874b83 commit 279a126

26 files changed

+2898
-254
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ $ yarn add @mutualmobile/react-native-barricade
3737

3838
**1. Create and start Barricade**
3939

40-
Create an instance of Barricade with the help of the `createBarricade` function along with an array of `RequestConfig` as its function argument.
40+
Create an instance of Barricade with the help of the `createBarricade` function. While calling this function, you can pass an array of `RequestConfig`(optional) to register the request configs. You can also register a request config later by making use of `registerRequest` method on the barricade instance.
4141

4242
**:warning: Make sure to do this in index.js so that you can start Barricade before hitting any API.**
4343

4444
```tsx
4545
import { createBarricade } from '@mutualmobile/react-native-barricade';
4646

47-
const requestConfig = []; // Array of RequestConfigs for all the APIs that needs to be mocked
47+
const requestConfig = []; // Optional: Array of RequestConfigs for all the APIs that needs to be mocked
4848
const barricade = createBarricade(requestConfig);
4949
barricade.start(); // Start the Barricade
5050

@@ -76,7 +76,25 @@ const App = () => {
7676

7777
**3. Create RequestConfigs**
7878

79-
Create a `RequestConfig` for each API you want to mock. Then, add these to the list of request configs shown in Step 1.
79+
Create a `RequestConfig` for each API you want to mock. Then, add these to the list of request configs shown in Step 1 or register them individually by calling the `registerRequest` method as shown below.
80+
81+
```tsx
82+
import { getBarricadeInstance } from '@mutualmobile/react-native-barricade';
83+
84+
const apiRequestConfig = {} // RequestConfig for a particular API that you wish to mock.
85+
getBarricadeInstance()?.registerRequest(apiRequestConfig);
86+
```
87+
88+
**:warning: Make sure to call the `registerRequest` method only after barricade instance is created.**
89+
90+
In case you want to unregister a config programmatically, you can do this by calling the `unregisterRequest` method similar to the registerRequest method.
91+
92+
```tsx
93+
import { getBarricadeInstance } from '@mutualmobile/react-native-barricade';
94+
95+
const apiRequestConfig = {} // RequestConfig object that was previously used for registering
96+
getBarricadeInstance()?.unregisterRequest(apiRequestConfig);
97+
```
8098

8199
**RequestConfig:**
82100
| Property | Description | Type |
@@ -86,6 +104,7 @@ Create a `RequestConfig` for each API you want to mock. Then, add these to the l
86104
| **`pathEvaluation`** | Data used to identify the current API triggered from the list of RequestConfigs. | `PathEvaluation` |
87105
| **`responseHandler`** | List of mocked responses the current API can return with. By default, the first response from the list is selected. | `ResponseHandler[]` |
88106
| **`delay`** | The time (in milliseconds) Barricade needs to wait before responding with the mocked response. This is optional and by default it's `400`. | `number` / `undefined` |
107+
| **`disabled`** | Boolean used to enable/disable mocking of current API. This is optional and by default it's `undefined`. | `boolean` / `undefined` |
89108

90109
**PathEvaluation:**
91110
| Property | Description | Type |
@@ -159,9 +178,9 @@ const successResponseHandler = (request: Request) => {
159178
Barricade comes packaged with an in-app interface that allows you to select the network responses at runtime. For this to be visible, you need to add the `BarricadeView` mentioned in Step 2 under **Usage**.
160179

161180
<p align="center">
162-
<img src="https://github.com/mutualmobile/react-native-barricade/blob/main/docs/screenshots/menu.png?raw=true" alt="Developer Menu" width="231" height="500"/>
163-
<img src="https://github.com/mutualmobile/react-native-barricade/blob/main/docs/screenshots/list.png?raw=true" alt="List View" width="231" height="500"/>
164-
<img src="https://github.com/mutualmobile/react-native-barricade/blob/main/docs/screenshots/detail.png?raw=true" alt="Detail View" width="231" height="500"/>
181+
<img src="https://github.com/mutualmobile/react-native-barricade/blob/main/docs/screenshots/developer-menu.png?raw=true" alt="Developer Menu" width="231" height="500"/>
182+
<img src="https://github.com/mutualmobile/react-native-barricade/blob/main/docs/screenshots/request-list.png?raw=true" alt="List View" width="231" height="500"/>
183+
<img src="https://github.com/mutualmobile/react-native-barricade/blob/main/docs/screenshots/response-list.png?raw=true" alt="Detail View" width="231" height="500"/>
165184
</p>
166185

167186
With this in place and the device shaken, you'll be able to see an option for `Barricade` in React Native's developer menu. On tapping the `Barricade` option, you’ll be redirected to a screen with the list of mocked APIs.
@@ -186,7 +205,8 @@ const App = () => {
186205
```
187206

188207
**Note:** In BarricadeView, apart from changing the selected response for any of the listed APIs, we can also:
189-
- Disable/Enable Barricade. This will stop/start mocking API calls and lets you check the app with the actual/mocked API responses at runtime.
208+
- Disable/Enable Barricade. This will stop/start mocking all the registered API calls and lets you check the app with the actual/mocked API responses at runtime.
209+
- Disable/Enable API Mock. This will stop/start mocking the current API calls and lets you check the app with the actual/mocked API response at runtime.
190210
- Reset all the changes done to the list of selected responses.
191211

192212
## Store Submission

docs/media/demo.gif

-5.18 MB
Loading

docs/screenshots/detail.png

-1.21 MB
Binary file not shown.
764 KB
Loading

docs/screenshots/list.png

-1.24 MB
Binary file not shown.

docs/screenshots/menu.png

-775 KB
Binary file not shown.

docs/screenshots/request-list.png

105 KB
Loading

docs/screenshots/response-list.png

105 KB
Loading

example/mock-server/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { createBarricade } from '@mutualmobile/react-native-barricade';
1+
import {
2+
createBarricade,
3+
getBarricadeInstance,
4+
} from '@mutualmobile/react-native-barricade';
25

36
import { RecentApiRequestConfig } from './api/recent.api.mock';
47
import { SearchApiRequestConfig } from './api/search.api.mock';
58

69
export const mockServer = () => {
7-
const barricade = createBarricade([
8-
RecentApiRequestConfig,
9-
SearchApiRequestConfig,
10-
]);
11-
barricade.start(); // We can all call this like - getBarricadeInstance()?.start();
10+
const barricade = createBarricade([RecentApiRequestConfig]);
11+
barricade.start(); // We can also call this like - getBarricadeInstance()?.start();
12+
getBarricadeInstance()?.registerRequest(SearchApiRequestConfig);
1213
};

src/__tests__/components/Footer.test.tsx

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,33 @@ import { TouchableOpacity } from 'react-native';
33
import renderer, { act } from 'react-test-renderer';
44

55
import { Footer } from '../../components/Footer';
6-
import { Barricade } from '../../network/barricade';
7-
import {
8-
firstApiConfig,
9-
secondApiConfig,
10-
thirdApiConfig,
11-
} from '../data/barricade-test-data';
12-
13-
let barricade: Barricade;
14-
beforeEach(() => {
15-
barricade = new Barricade([firstApiConfig, secondApiConfig, thirdApiConfig]);
16-
barricade.start();
17-
});
18-
19-
afterEach(() => {
20-
jest.clearAllMocks();
21-
barricade.shutdown();
22-
});
23-
24-
describe('given that Barricade is enabled,', () => {
25-
test('should match Footer snapshot', () => {
26-
const tree = renderer.create(<Footer barricade={barricade} />);
27-
expect(tree).toMatchSnapshot();
28-
expect.assertions(1);
29-
30-
barricade.shutdown();
31-
});
32-
33-
test('when "Disable barricade" button is pressed, should disable barricade', async () => {
34-
barricade.shutdown = jest.fn();
35-
const tree = renderer.create(<Footer barricade={barricade} />);
36-
const instance = tree.root;
37-
await act(() => instance.findByType(TouchableOpacity).props.onPress());
38-
39-
expect(barricade.shutdown).toHaveBeenCalled();
40-
expect.assertions(1);
41-
});
6+
import { Strings } from '../../constants';
7+
import { LightThemeColors } from '../../theme/colors';
8+
9+
test('should match Footer snapshot', () => {
10+
const tree = renderer.create(
11+
<Footer
12+
title={Strings.DisableBarricade}
13+
titleStyle={{ color: LightThemeColors.error }}
14+
onPress={jest.fn}
15+
/>,
16+
);
17+
expect(tree).toMatchSnapshot();
18+
expect.assertions(1);
4219
});
4320

44-
describe('given that Barricade is disabled,', () => {
45-
test('given that Barricade is disabled, should match Footer snapshot', () => {
46-
barricade.shutdown();
47-
48-
const tree = renderer.create(<Footer barricade={barricade} />);
49-
expect(tree).toMatchSnapshot();
50-
51-
expect.assertions(1);
52-
});
53-
54-
test('when "Enable barricade" button is pressed, should enable barricade', async () => {
55-
barricade.shutdown();
56-
barricade.start = jest.fn();
57-
const tree = renderer.create(<Footer barricade={barricade} />);
58-
const instance = tree.root;
59-
await act(() => instance.findByType(TouchableOpacity).props.onPress());
60-
61-
expect(barricade.start).toHaveBeenCalledTimes(1);
62-
expect.assertions(1);
63-
});
21+
test('given that onPress is passed to Footer, when its pressed should call the onPress prop', async () => {
22+
const onPress = jest.fn();
23+
const tree = renderer.create(
24+
<Footer
25+
onPress={onPress}
26+
title={Strings.DisableBarricade}
27+
titleStyle={{ color: LightThemeColors.error }}
28+
/>,
29+
);
30+
const instance = tree.root;
31+
await act(() => instance.findByType(TouchableOpacity).props.onPress());
32+
33+
expect(onPress).toHaveBeenCalled();
34+
expect.assertions(1);
6435
});

0 commit comments

Comments
 (0)