[go: nahoru, domu]

Skip to content

Commit

Permalink
ui: create component to list namespaces
Browse files Browse the repository at this point in the history
This commit creates a component to separate the code into another component.

Signed-off-by: Leonardo R.S. Joao <leonardo.joao@ossystems.com.br>
  • Loading branch information
leonardojoao authored and gustavosbarreto committed Feb 7, 2022
1 parent 33c2c86 commit af491cb
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 72 deletions.
21 changes: 4 additions & 17 deletions ui/src/components/namespace/Namespace.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@
mdi-chevron-down
</v-icon>

<v-list-item
v-for="item in namespaces"
:key="item.tenant_id"
link
@click="switchIn(item.tenant_id)"
>
<v-list-item-content>
<v-list-item-title
:data-test="item.name+'-namespace'"
v-text="item.name"
/>
</v-list-item-content>
</v-list-item>
<NamespaceList data-test="namespaceList-component" />

<v-list-item v-if="isEnterprise">
<NamespaceAdd data-test="namespaceAdd-component" />
Expand All @@ -46,12 +34,15 @@
</template>

<script>
import NamespaceList from '@/components/namespace/NamespaceList';
import NamespaceAdd from '@/components/namespace/NamespaceAdd';
export default {
name: 'NamespaceMenuComponent',
components: {
NamespaceList,
NamespaceAdd,
},
Expand All @@ -66,10 +57,6 @@ export default {
return this.$store.getters['namespaces/get'];
},
namespaces() {
return this.$store.getters['namespaces/list'].filter((el) => el.name !== this.namespace.name);
},
hasNamespace() {
return this.$store.getters['namespaces/getNumberNamespaces'] !== 0;
},
Expand Down
48 changes: 48 additions & 0 deletions ui/src/components/namespace/NamespaceList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<template>
<fragment>
<v-list-item
v-for="item in namespaces"
:key="item.tenant_id"
link
@click="switchIn(item.tenant_id)"
>
<v-list-item-content>
<v-list-item-title
:data-test="item.name+'-namespace'"
v-text="item.name"
/>
</v-list-item-content>
</v-list-item>
</fragment>
</template>

<script>
export default {
name: 'NamespaceList',
computed: {
namespace() {
return this.$store.getters['namespaces/get'];
},
namespaces() {
return this.$store.getters['namespaces/list'].filter((el) => el.name !== this.namespace.name);
},
},
methods: {
async switchIn(tenant) {
try {
await this.$store.dispatch('namespaces/switchNamespace', {
tenant_id: tenant,
});
window.location.reload();
} catch {
this.$store.dispatch('snackbar/showSnackbarErrorLoading', this.$errors.snackbar.namespaceSwitch);
}
},
},
};
</script>
37 changes: 2 additions & 35 deletions ui/tests/unit/components/namespaces/Namespace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,12 @@ describe('Namespace', () => {
tenant_id: 'e359bf484715',
};

const namespaces = [
{
name: 'namespace1',
owner: 'user1',
member_names: ['user3', 'user4', 'user5'],
tenant_id: 'xxxxxxxx',
},
{
name: 'namespace2',
owner: 'user1',
member_names: ['user3', 'user4'],
tenant_id: 'xxxxxxxy',
},
];

const store = new Vuex.Store({
namespaced: true,
state: {
namespace,
namespaces,
},
getters: {
'namespaces/list': (state) => state.namespaces,
'namespaces/get': (state) => state.namespace,
},
actions: {
Expand Down Expand Up @@ -99,7 +82,6 @@ describe('Namespace', () => {
});
it('Process data in the computed', () => {
expect(wrapper.vm.namespace).toEqual(namespace);
expect(wrapper.vm.namespaces).toEqual(namespaces.filter((el) => el.name !== namespace.name));
expect(wrapper.vm.hasNamespace).toEqual(true);
expect(wrapper.vm.tenant).toEqual(namespace.tenant_id);
});
Expand All @@ -109,17 +91,10 @@ describe('Namespace', () => {
//////

it('Renders the template with components', () => {
expect(wrapper.find('[data-test="namespaceList-component"]').exists()).toEqual(true);
expect(wrapper.find('[data-test="namespaceAdd-component"]').exists()).toEqual(true);
expect(wrapper.find('[data-test="namespaceAddNoNamespace-component"]').exists()).toEqual(false);
});

it('Renders the template with data', async () => {
const namespacesLocal = namespaces.filter((el) => el.name !== namespace.name);

Object.keys(namespacesLocal).forEach((item) => {
expect(wrapper.find(`[data-test="${namespacesLocal[item].name}-namespace"]`).text()).toEqual(namespacesLocal[item].name);
});
});
});

///////
Expand Down Expand Up @@ -168,7 +143,6 @@ describe('Namespace', () => {
});
it('Process data in the computed', () => {
expect(wrapper.vm.namespace).toEqual(namespace);
expect(wrapper.vm.namespaces).toEqual(namespaces.filter((el) => el.name !== namespace.name));
expect(wrapper.vm.hasNamespace).toEqual(true);
expect(wrapper.vm.tenant).toEqual(namespace.tenant_id);
});
Expand All @@ -178,16 +152,9 @@ describe('Namespace', () => {
//////

it('Renders the template with components', () => {
expect(wrapper.find('[data-test="namespaceList-component"]').exists()).toEqual(true);
expect(wrapper.find('[data-test="namespaceAdd-component"]').exists()).toEqual(false);
expect(wrapper.find('[data-test="namespaceAddNoNamespace-component"]').exists()).toEqual(false);
});

it('Renders the template with data', async () => {
const namespacesLocal = namespaces.filter((el) => el.name !== namespace.name);

Object.keys(namespacesLocal).forEach((item) => {
expect(wrapper.find(`[data-test="${namespacesLocal[item].name}-namespace"]`).text()).toEqual(namespacesLocal[item].name);
});
});
});
});
147 changes: 147 additions & 0 deletions ui/tests/unit/components/namespaces/NamespaceList.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import Vuex from 'vuex';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import Vuetify from 'vuetify';
import NamespaceList from '@/components/namespace/NamespaceList';

describe('Namespace', () => {
const localVue = createLocalVue();
const vuetify = new Vuetify();
localVue.use(Vuex);

let wrapper;

const namespace = {
name: 'namespace3',
owner: 'user1',
member_names: ['user6', 'user7', 'user8'],
tenant_id: 'e359bf484715',
};

const namespaces = [
{
name: 'namespace1',
owner: 'user1',
member_names: ['user3', 'user4', 'user5'],
tenant_id: 'xxxxxxxx',
},
{
name: 'namespace2',
owner: 'user1',
member_names: ['user3', 'user4'],
tenant_id: 'xxxxxxxy',
},
];

const store = new Vuex.Store({
namespaced: true,
state: {
namespace,
namespaces,
},
getters: {
'namespaces/list': (state) => state.namespaces,
'namespaces/get': (state) => state.namespace,
},
actions: {
'namespaces/switchNamespace': () => {},
'snackbar/showSnackbarErrorLoading': () => {},
},
});

///////
// In this case, check owner fields rendering in enterprise version.
///////

describe('Enterprise version', () => {
beforeEach(() => {
wrapper = shallowMount(NamespaceList, {
store,
localVue,
stubs: ['fragment'],
vuetify,
});
});

///////
// Component Rendering
//////

it('Is a Vue Instance', () => {
expect(wrapper).toBeTruthy();
});
it('Renders the component', () => {
expect(wrapper.html()).toMatchSnapshot();
});

///////
// Data and Props checking
//////

it('Process data in the computed', () => {
expect(wrapper.vm.namespace).toEqual(namespace);
expect(wrapper.vm.namespaces).toEqual(namespaces.filter((el) => el.name !== namespace.name));
});

//////
// HTML validation
//////

it('Renders the template with data', async () => {
const namespacesLocal = namespaces.filter((el) => el.name !== namespace.name);

Object.keys(namespacesLocal).forEach((item) => {
expect(wrapper.find(`[data-test="${namespacesLocal[item].name}-namespace"]`).text()).toEqual(namespacesLocal[item].name);
});
});
});

///////
// In this case, check owner fields rendering in open version
// of the template.
///////

describe('Open version', () => {
beforeEach(() => {
wrapper = shallowMount(NamespaceList, {
store,
localVue,
stubs: ['fragment', 'router-link'],
vuetify,
});

jest.spyOn(Storage.prototype, 'getItem').mockReturnValue(namespace.tenant_id);
});

///////
// Component Rendering
//////

it('Is a Vue Instance', () => {
expect(wrapper).toBeTruthy();
});
it('Renders the component', () => {
expect(wrapper.html()).toMatchSnapshot();
});

///////
// Data and Props checking
//////

it('Process data in the computed', () => {
expect(wrapper.vm.namespace).toEqual(namespace);
expect(wrapper.vm.namespaces).toEqual(namespaces.filter((el) => el.name !== namespace.name));
});

//////
// HTML validation
//////

it('Renders the template with data', async () => {
const namespacesLocal = namespaces.filter((el) => el.name !== namespace.name);

Object.keys(namespacesLocal).forEach((item) => {
expect(wrapper.find(`[data-test="${namespacesLocal[item].name}-namespace"]`).text()).toEqual(namespacesLocal[item].name);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ exports[`Namespace Enterprise version Renders the component 1`] = `
<v-icon-stub color="primary">
mdi-chevron-down
</v-icon-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace1-namespace">namespace1</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace2-namespace">namespace2</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
<namespacelist-stub data-test="namespaceList-component"></namespacelist-stub>
<v-list-item-stub activeclass="" tag="div">
<namespaceadd-stub data-test="namespaceAdd-component"></namespaceadd-stub>
</v-list-item-stub>
Expand All @@ -32,16 +23,7 @@ exports[`Namespace Open version Renders the component 1`] = `
<v-icon-stub color="primary">
mdi-chevron-down
</v-icon-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace1-namespace">namespace1</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace2-namespace">namespace2</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
<namespacelist-stub data-test="namespaceList-component"></namespacelist-stub>
<!---->
</v-list-group-stub>
</v-list-stub>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Namespace Enterprise version Renders the component 1`] = `
<fragment-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace1-namespace">namespace1</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace2-namespace">namespace2</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
</fragment-stub>
`;

exports[`Namespace Open version Renders the component 1`] = `
<fragment-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace1-namespace">namespace1</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
<v-list-item-stub activeclass="" link="true" tag="div">
<v-list-item-content-stub tag="div">
<v-list-item-title-stub tag="div" data-test="namespace2-namespace">namespace2</v-list-item-title-stub>
</v-list-item-content-stub>
</v-list-item-stub>
</fragment-stub>
`;

0 comments on commit af491cb

Please sign in to comment.