Skip to content

Commit 406b985

Browse files
Add support for handing props to the component in LDRouteGuard (#61)
Co-authored-by: Benjamin Frost <[email protected]>
1 parent 1f5dd18 commit 406b985

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export default {
104104

105105
### LDRouteGuard Component
106106

107-
Use this as an intermediary component on a route you need to guard with a feature flag; it is based on the `ldRedirectMixin`. All props are passed to the component rendered.
107+
Use this as an intermediary component on a route you need to guard with a feature flag; it is based on the `ldRedirectMixin`.
108108

109109
```javascript
110110
import { LDRouteGuard } from 'vue-ld';
@@ -115,6 +115,7 @@ const route = {
115115
component: LDRouteGuard,
116116
props: {
117117
component: SecretComponent,
118+
componentProps: { exampleProp: true },
118119
requiredFeatureFlag: 'palantir',
119120
to: { name: 'away' },
120121
},
@@ -123,12 +124,13 @@ const route = {
123124

124125
#### Props
125126

126-
| key | description | type |
127-
| :------------- | ---------------------------------------------------------------------------------------------- | --------------------------------- |
128-
| `component` | The component to be rendered given the required feature flag is true. | `vue component` |
129-
| `requiredFlag` | If the given feature flag is false, the user will be redirected to the given route. | `string` |
130-
| `to` | The path which vue router will push. Functions passed are expected to resolve to a valid path. | `string`, `object`, or `function` |
131-
| `invertFlag` | If set to true the the inverse of the requiredFlag's value will be used. | `boolean` |
127+
| key | description | type |
128+
| :--------------- | ---------------------------------------------------------------------------------------------- | --------------------------------- |
129+
| `component` | The component to be rendered given the required feature flag is true. | `vue component` |
130+
| `componentProps` | The props object to hand to the component above. | `object` |
131+
| `requiredFlag` | If the given feature flag is false, the user will be redirected to the given route. | `string` |
132+
| `to` | The path which vue router will push. Functions passed are expected to resolve to a valid path. | `string`, `object`, or `function` |
133+
| `invertFlag` | If set to true the the inverse of the requiredFlag's value will be used. | `boolean` |
132134

133135
## Development
134136

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-ld",
3-
"version": "0.1.11",
3+
"version": "0.2.0",
44
"description": "A Vue.js wrapper for the LaunchDarkly SDK for Browser JavaScript",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.es.js",

src/components/LDRouteGuard.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<component :is="importedComponent" v-if="show" v-bind="$props"></component>
2+
<component :is="importedComponent" v-if="show" v-bind="componentProps"></component>
33
</template>
44

55
<script>
@@ -9,6 +9,7 @@ export default {
99
mixins: [ldRedirectMixin()],
1010
props: {
1111
component: { type: [Function, Object, Promise], required: true },
12+
componentProps: { type: Object, required: false, default: () => {} },
1213
requiredFeatureFlag: { type: String, required: true },
1314
to: { type: [String, Object, Function], required: true },
1415
invertFlag: { type: Boolean, required: false, default: false },

tests/unit/ld-route-guard.spec.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ const mixins = [ldRedirect('myFlag', '/')];
1111

1212
const EmptyComponent = {
1313
name: 'empty-component',
14-
render(h) {
15-
return h('div');
14+
props: {
15+
title: { type: String, require: false, default: 'title' },
16+
},
17+
render(createElement) {
18+
return createElement('div', this.title);
1619
},
1720
};
1821

@@ -27,7 +30,7 @@ describe('ldRedirectMixin', () => {
2730
let localVue;
2831
let mocks;
2932
let wrapper;
30-
const finishSetup = async (component, invertFlag) => {
33+
const finishSetup = async (component, invertFlag, props = {}) => {
3134
localVue = createLocalVue();
3235
localVue.use(VueLd, vueLdOptions);
3336
mocks = {
@@ -39,6 +42,7 @@ describe('ldRedirectMixin', () => {
3942
mocks,
4043
propsData: {
4144
component,
45+
componentProps: props,
4246
requiredFeatureFlag: 'myFlag',
4347
to: '/',
4448
invertFlag,
@@ -79,6 +83,19 @@ describe('ldRedirectMixin', () => {
7983
await finishSetup(EmptyComponent, false);
8084
expect(wrapper.vm.$router.push).not.toHaveBeenCalled();
8185
expect(wrapper.findComponent(EmptyComponent).exists()).toBe(true);
86+
expect(wrapper.findComponent(EmptyComponent).html()).toBe('<div>title</div>');
87+
});
88+
89+
it('does not redirect with feature flag & contains component with props', async () => {
90+
server.respondWith([
91+
200,
92+
{ 'Content-Type': 'application/json' },
93+
JSON.stringify(flagsResponse),
94+
]);
95+
await finishSetup(EmptyComponent, false, { title: 'a different title' });
96+
expect(wrapper.vm.$router.push).not.toHaveBeenCalled();
97+
expect(wrapper.findComponent(EmptyComponent).exists()).toBe(true);
98+
expect(wrapper.findComponent(EmptyComponent).html()).toBe('<div>a different title</div>');
8299
});
83100

84101
it('redirects with featureflag if invertFlag is set', async () => {

0 commit comments

Comments
 (0)