Skip to content

Commit

Permalink
chore: removed unnecessary state
Browse files Browse the repository at this point in the history
  • Loading branch information
wouteraj committed May 17, 2021
1 parent 1f3d0c2 commit 247c02c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/nde-erfgoed-manage/lib/app-root.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ export class AppRootComponent extends RxLitElement {
<div slot="title">${this.translator?.translate('nde.navigation.collections.title')}</div>
<div slot="actions" @click="${() => this.actor.send(AppEvents.CLICKED_CREATE_COLLECTION)}">${ unsafeSVG(Plus) }</div>
</nde-sidebar-list-item>
${this.collections?.map((collection) => html`<nde-sidebar-list-item slot="item" inverse ?selected="${ collection.uri === this.selected.uri}" @click="${() => this.actor.send(CollectionEvents.SELECTED_COLLECTION, { collection })}"><div slot="title">${collection.name}</div></nde-sidebar-list-item>`)}
${this.collections?.map((collection) => html`<nde-sidebar-list-item slot="item" inverse ?selected="${ collection.uri === this.selected?.uri}" @click="${() => this.actor.send(CollectionEvents.SELECTED_COLLECTION, { collection })}"><div slot="title">${collection.name}</div></nde-sidebar-list-item>`)}
</nde-sidebar-list>
</nde-sidebar>
` : '' }
${ this.state?.matches({ [AppRootStates.FEATURE]: AppFeatureStates.AUTHENTICATE }) ? html`<nde-authenticate-root .actor='${this.actor.children.get(AppActors.AUTHENTICATE_MACHINE)}' .logger='${this.logger}' .translator='${this.translator}'></nde-authenticate-root>` : '' }
${ this.state?.matches({ [AppRootStates.FEATURE]: AppFeatureStates.COLLECTION }) ? html`<nde-collection-root .actor='${this.actor.children.get(AppActors.COLLECTION_MACHINE)}' .showDelete='${this.collections.length > 1}' .logger='${this.logger}' .translator='${this.translator}'></nde-collection-root>` : '' }
${ this.state?.matches({ [AppRootStates.FEATURE]: AppFeatureStates.COLLECTION }) ? html`<nde-collection-root .actor='${this.actor.children.get(AppActors.COLLECTION_MACHINE)}' .showDelete='${this.collections?.length > 1}' .logger='${this.logger}' .translator='${this.translator}'></nde-collection-root>` : '' }
`;
}
Expand Down
19 changes: 2 additions & 17 deletions packages/nde-erfgoed-manage/lib/app.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export enum AppFeatureStates {
export enum AppDataStates {
IDLE = '[AppCreationStates: Idle]',
REFRESHING = '[AppCreationStates: Refreshing]',
REFRESHING_SIDEBAR = '[AppCreationStates: Refreshing Sidebar]',
CREATING = '[AppCreationStates: Creating]',
}

Expand Down Expand Up @@ -245,7 +244,7 @@ export const appMachine = (
[AppEvents.CLICKED_CREATE_COLLECTION]: AppDataStates.CREATING,
[AppEvents.LOGGED_IN]: AppDataStates.REFRESHING,
[CollectionEvents.CLICKED_DELETE]: AppDataStates.REFRESHING,
[CollectionEvents.SAVED_COLLECTION]: AppDataStates.REFRESHING_SIDEBAR,
[CollectionEvents.SAVED_COLLECTION]: AppDataStates.REFRESHING,
},
},
/**
Expand All @@ -264,7 +263,7 @@ export const appMachine = (
setCollections,
send((context, event) => ({
type: CollectionEvents.SELECTED_COLLECTION,
collection: event.data[0],
collection: context.selected ? context.selected : event.data[0],
})),
],
cond: (context, event) => event.data.length > 0,
Expand All @@ -275,20 +274,6 @@ export const appMachine = (
],
},
},
[AppDataStates.REFRESHING_SIDEBAR]: {
invoke: {
src: () => collectionStore.all(),
onDone: [
{
target: AppDataStates.IDLE,
actions: [ setCollections ],
},
{
target: AppDataStates.CREATING,
},
],
},
},
/**
* Creating a new collection.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Alert } from '@digita-ai/nde-erfgoed-components';
import { Alert, formMachine } from '@digita-ai/nde-erfgoed-components';
import { ArgumentError, Collection, CollectionObjectMemoryStore, ConsoleLogger, LoggerLevel, MemoryStore } from '@digita-ai/nde-erfgoed-core';
import { interpret, Interpreter } from 'xstate';
import { AppEvents } from '../../app.events';
Expand Down Expand Up @@ -133,6 +133,56 @@ describe('CollectionRootComponent', () => {

});

it('should send event when collection title is clicked', async () => {

machine.onTransition((state) => {

machine.send = jest.fn();

if(state.matches(CollectionStates.IDLE) && state.context?.collection) {

const button = window.document.body.getElementsByTagName('nde-collection-root')[0].shadowRoot.querySelector('nde-form-element[slot="title"]') as HTMLElement;
button.click();

expect(machine.send).toHaveBeenCalledTimes(1);

}

});

machine.start();
machine.send(CollectionEvents.SELECTED_COLLECTION, { collection: collection1 });

window.document.body.appendChild(component);
await component.updateComplete;

});

it('should send event when collection subtitle is clicked', async () => {

machine.onTransition((state) => {

machine.send = jest.fn();

if(state.matches(CollectionStates.IDLE) && state.context?.collection) {

const button = window.document.body.getElementsByTagName('nde-collection-root')[0].shadowRoot.querySelector('nde-form-element[slot="subtitle"]') as HTMLElement;
button.click();

expect(machine.send).toHaveBeenCalledTimes(1);

}

});

machine.start();
machine.send(CollectionEvents.SELECTED_COLLECTION, { collection: collection1 });

window.document.body.appendChild(component);
await component.updateComplete;

});

it('should send event when create is clicked', async () => {

machine.onTransition((state) => {
Expand Down Expand Up @@ -292,4 +342,23 @@ describe('CollectionRootComponent', () => {

});

it('should update subscription when formActor is updated', async () => {

machine.start();

window.document.body.appendChild(component);
await component.updateComplete;

component.subscribe = jest.fn();

const map = new Map<string, string>();
map.set('actor', 'bla');
map.set('formActor', 'bla');

component.updated(map);

expect(component.subscribe).toHaveBeenCalledTimes(5);

});

});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { html, property, PropertyValues, internalProperty, unsafeCSS, css, TemplateResult, CSSResult } from 'lit-element';
import { ArgumentError, Collection, CollectionObject, Logger, Translator } from '@digita-ai/nde-erfgoed-core';
import { FormEvent, FormActors, FormSubmissionStates, FormEvents, Alert } from '@digita-ai/nde-erfgoed-components';
import { map, tap } from 'rxjs/operators';
import { map } from 'rxjs/operators';
import { from } from 'rxjs';
import { ActorRef, Interpreter, State } from 'xstate';
import { RxLitElement } from 'rx-lit';
import { Collection as CollectionIcon, Cross, Edit, Empty, Object as ObjectIcon, Plus, Save, Theme, Trash } from '@digita-ai/nde-erfgoed-theme';
import { Collection as CollectionIcon, Cross, Empty, Object as ObjectIcon, Plus, Save, Theme, Trash } from '@digita-ai/nde-erfgoed-theme';
import { unsafeSVG } from 'lit-html/directives/unsafe-svg';
import { AppEvents } from '../../app.events';
import { CollectionContext, CollectionStates } from './collection.machine';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Collection, CollectionObjectMemoryStore, CollectionObjectStore, MemoryStore, Store } from '@digita-ai/nde-erfgoed-core';
import { Collection, CollectionObjectMemoryStore, CollectionObjectStore, ConsoleLogger, LoggerLevel, MemoryStore, Store } from '@digita-ai/nde-erfgoed-core';
import { interpret, Interpreter } from 'xstate';
import { AppEvents } from '../../app.events';
import { appMachine } from '../../app.machine';
import { SolidMockService } from '../../common/solid/solid-mock.service';
import { addAlert, CollectionEvents, SelectedCollectionEvent } from './collection.events';
import { CollectionContext, collectionMachine, CollectionStates } from './collection.machine';

Expand Down Expand Up @@ -44,6 +46,13 @@ describe('CollectionMachine', () => {
collection: collection1,
}));

machine.parent = interpret(appMachine(
new SolidMockService(new ConsoleLogger(LoggerLevel.silly, LoggerLevel.silly)),
collectionStore,
objectStore,
{}
));

});

it('should be correctly instantiated', () => {
Expand Down Expand Up @@ -119,30 +128,30 @@ describe('CollectionMachine', () => {

});

// it('should save collection when saving', async (done) => {
it('should save collection when saving', async (done) => {

// collectionStore.save = jest.fn().mockResolvedValueOnce(collection1);
collectionStore.save = jest.fn().mockResolvedValueOnce(collection1);

// machine.onTransition((state) => {
machine.onTransition((state) => {

// if(state.matches(CollectionStates.IDLE) && state.context?.collection) {
if(state.matches(CollectionStates.IDLE) && state.context?.collection) {

// expect(collectionStore.save).toHaveBeenCalledTimes(1);
expect(collectionStore.save).toHaveBeenCalledTimes(1);

// expect(collectionStore.save).toHaveBeenCalledWith(collection1);
expect(collectionStore.save).toHaveBeenCalledWith(collection1);

// done();
done();

// }
}

// });
});

// machine.start();
// machine.send(CollectionEvents.SELECTED_COLLECTION, { collection: collection1 });
machine.start();
machine.send(CollectionEvents.SELECTED_COLLECTION, { collection: collection1 });

// machine.send(CollectionEvents.CLICKED_SAVE);
machine.send(CollectionEvents.CLICKED_SAVE);

// });
});

it('should assign when selected collection', async (done) => {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export const collectionMachine = (
target: CollectionStates.IDLE,
actions: [
sendParent((context) => ({ type: CollectionEvents.CLICKED_DELETE, collection: context.collection })),
sendParent((context) => ({ type: CollectionEvents.SELECTED_COLLECTION, collection: undefined })),
],
},
onError: {
Expand Down
8 changes: 4 additions & 4 deletions packages/nde-erfgoed-manage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@
],
"coverageThreshold": {
"global": {
"branches": 85.23,
"lines": 91.37,
"statements": 91.41,
"functions": 81.82
"branches": 85.43,
"functions": 84.3,
"lines": 92.16,
"statements": 92.15
}
},
"automock": false,
Expand Down

0 comments on commit 247c02c

Please sign in to comment.