Skip to content

Commit

Permalink
Moved samples to mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Oct 12, 2023
1 parent 7cd359a commit d1e83d6
Show file tree
Hide file tree
Showing 28 changed files with 314 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { EntryRecord } from '@holochain-open-dev/utils';
import { toPromise } from '@holochain-open-dev/stores';

import { {{pascal_case referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/types.js';
import { setup, sample{{pascal_case referenceable.name}} } from './common.js';
import { sample{{pascal_case referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/mocks.js';
import { setup } from './common.js';

test('create a {{pascal_case referenceable.name}} and get {{lower_case collection_name}}', async () => {
await runScenario(async scenario => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{#merge_scope previous_file_content (concat "export class " (pascal_case app_name) "ZomeMock extends ZomeMock implements AppAgentClient {" ) }}
{{previous_scope_content}}

{{#if (eq collection_type.type "ByAuthor")}}
async get_{{snake_case collection_name}}(author: AgentPubKey): Promise<Array<Record>> {
const actionHashes: {{referenceable.hash_type}}[] = this.{{camel_case referenceable.name}}.authorMap.get(author);
return actionHashes.map(actionHash => this.{{camel_case referenceable.name}}.entryRecord(actionHash)).map(er => er?.record).filter(r => !!r) as Record[];
}

{{else}}
async get_{{snake_case collection_name}}(_: any): Promise<Array<Record>> {
return this.{{camel_case referenceable.name}}.entryRecords.map(er => er?.record).filter(r => !!r) as Record[];
}
{{/if}}

{{/merge_scope}}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
fixt = "*"
futures = { version = "0.3.1", default-features = false }
hdk = { workspace = true, features = ["encoding", "test_utils"] }
holochain = { version = "0.2", default-features = false, features = [
"test_utils",
] }
holochain = { workspace = true }
tokio = { version = "1.3", features = ["full"] }

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
AgentPubKeyMap,
decodeEntry,
fakeEntry,
fakeCreateAction,
fakeUpdateEntry,
fakeDeleteEntry,
fakeRecord,
pickBy,
ZomeMock,
RecordBag,
entryState,
HoloHashMap,
HashType,
hash
} from "@holochain-open-dev/utils";
import {
decodeHashFromBase64,
AgentPubKey,
ActionHash,
EntryHash,
AppAgentClient,
fakeAgentPubKey,
fakeDnaHash,
fakeActionHash,
fakeEntryHash,
Record,
} from "@holochain/client";
import { {{pascal_case app_name}}Client } from './{{kebab_case app_name}}-client.js'

export class {{pascal_case app_name}}ZomeMock extends ZomeMock implements AppAgentClient {
constructor(
myPubKey?: AgentPubKey
) {
super("{{snake_case app_name}}_test", "{{snake_case app_name}}", myPubKey);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { EntryRecord } from '@holochain-open-dev/utils';
import { toPromise } from '@holochain-open-dev/stores';

import { {{pascal_case entry_type.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/types.js';
import { setup, sample{{pascal_case entry_type.name}} } from './common.js';
import { sample{{pascal_case entry_type.name}} } from '../../../../ui/src/mocks.js';
import { setup } from './common.js';

test('create {{pascal_case entry_type.name}}', async () => {
await runScenario(async scenario => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { {{pascal_case entry_type.name}} } from './types.js';

{{#merge_scope previous_file_content (concat "export class " (pascal_case app_name) "ZomeMock extends ZomeMock implements AppAgentClient {" ) }}
{{previous_scope_content}}
/** {{title_case entry_type.name}} */
{{camel_case entry_type.name}} = new RecordBag<{{pascal_case entry_type.name}}>();
{{#each entry_type.fields}}
{{#if linked_from}}
{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}} = new HoloHashMap<ActionHash, ActionHash[]>();
{{/if}}
{{/each}}

async create_{{snake_case entry_type.name}}({{camel_case entry_type.name}}: {{pascal_case entry_type.name}}): Promise<Record> {
const record = fakeRecord(fakeCreateAction(hash({{camel_case entry_type.name}}, HashType.ENTRY)), fakeEntry({{camel_case entry_type.name}}));

this.{{camel_case entry_type.name}}.add([record]);

{{#each entry_type.fields}}
{{#if linked_from}}
{{#if (eq cardinality "single")}}
const existing{{pascal_case field_name}} = this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.get({{camel_case ../entry_type.name}}.{{field_name}}) || [];
this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.set({{camel_case ../entry_type.name}}.{{field_name}}, [...existing{{pascal_case field_name}}, record.signed_action.hashed.hash]);
{{/if}}
{{#if (eq cardinality "option")}}
if ({{camel_case ../entry_type.name}}.{{field_name}}) {
const existing{{pascal_case field_name}} = this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.get({{camel_case ../entry_type.name}}.{{field_name}}) || [];
this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.set({{camel_case ../entry_type.name}}.{{field_name}}, [...existing{{pascal_case field_name}}, record.signed_action.hashed.hash]);
}
{{/if}}
{{#if (eq cardinality "vector")}}
for (const {{field_name}} of {{camel_case ../entry_type.name}}.{{field_name}}) {
const existing{{pascal_case field_name}} = this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.get({{field_name}}) || [];
this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.set({{field_name}}, [...existing{{pascal_case field_name}}, record.signed_action.hashed.hash]);
}
{{/if}}
{{/if}}
{{/each}}

return record;
}

async get_{{snake_case entry_type.name}}({{camel_case entry_type.name}}Hash: {{#if entry_type.reference_entry_hash}}EntryHash{{else}}ActionHash{{/if}}): Promise<Record | undefined> {
const state = entryState(this.{{camel_case entry_type.name}}, {{camel_case entry_type.name}}Hash);

if (!state || state.deleted) return undefined;

return state.lastUpdate?.record;
}

{{#if crud.delete}}
async delete_{{snake_case entry_type.name}}(original_{{snake_case entry_type.name}}_hash: ActionHash): Promise<ActionHash> {
const record = fakeRecord(fakeDeleteEntry(original_{{snake_case entry_type.name}}_hash));

this.{{camel_case entry_type.name}}.add([record]);

return record.signed_action.hashed.hash;
}
{{/if}}

{{#if crud.update}}
async update_{{snake_case entry_type.name}}(input: { {{#if link_from_original_to_each_update}}original_{{snake_case entry_type.name}}_hash: ActionHash; {{/if}}previous_{{snake_case entry_type.name}}_hash: ActionHash; updated_{{snake_case entry_type.name}}: {{pascal_case entry_type.name}}; }): Promise<Record> {
const record = fakeRecord(fakeUpdateEntry(input.previous_{{snake_case entry_type.name}}_hash, fakeEntry(input.updated_{{snake_case entry_type.name}})), fakeEntry(input.updated_{{snake_case entry_type.name}}));

this.{{camel_case entry_type.name}}.add([record]);

const {{camel_case entry_type.name}} = input.updated_{{snake_case entry_type.name}};

{{#each entry_type.fields}}
{{#if linked_from}}
{{#if (eq cardinality "single")}}
const existing{{pascal_case field_name}} = this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.get({{camel_case ../entry_type.name}}.{{field_name}}) || [];
this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.set({{camel_case ../entry_type.name}}.{{field_name}}, [...existing{{pascal_case field_name}}, record.signed_action.hashed.hash]);
{{/if}}
{{#if (eq cardinality "option")}}
if ({{camel_case ../entry_type.name}}.{{field_name}}) {
const existing{{pascal_case field_name}} = this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.get({{camel_case ../entry_type.name}}.{{field_name}}) || [];
this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.set({{camel_case ../entry_type.name}}.{{field_name}}, [...existing{{pascal_case field_name}}, record.signed_action.hashed.hash]);
}
{{/if}}
{{#if (eq cardinality "vector")}}
for (const {{field_name}} of {{camel_case ../entry_type.name}}.{{field_name}}) {
const existing{{pascal_case field_name}} = this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.get({{field_name}}) || [];
this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.set({{field_name}}, [...existing{{pascal_case field_name}}, record.signed_action.hashed.hash]);
}
{{/if}}
{{/if}}
{{/each}}

return record;
}
{{/if}}

{{#each entry_type.fields}}
{{#if linked_from}}
async get_{{snake_case (plural ../entry_type.name)}}_for_{{snake_case linked_from.name}}({{camel_case linked_from.singular_arg}}: {{linked_from.hash_type}}): Promise<Array<Record>> {
const actionHashes: ActionHash[] = this.{{camel_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}}.get({{camel_case linked_from.singular_arg}}) || [];

return actionHashes.map(actionHash => this.{{camel_case ../entry_type.name}}.entryRecord(actionHash)?.record).filter(r => !!r) as Record[];
}

{{/if}}
{{/each}}

{{/merge_scope}}

export async function sample{{pascal_case entry_type.name}}(client: {{pascal_case app_name}}Client, partial{{pascal_case entry_type.name}}: Partial<{{pascal_case entry_type.name}}> = {}): Promise<{{ pascal_case entry_type.name }}> {
return {
...{
{{#each entry_type.fields}}
{{#if linked_from}}
{{#if (ne linked_from.hash_type "AgentPubKey")}}
{{#if (eq cardinality "vector")}}
{{#if (eq (pascal_case linked_from.name) (pascal_case ../entry_type.name))}}
{{field_name}}: [],
{{else}}
{{#if (eq linked_from.hash_type "ActionHash")}}
{{field_name}}: partial{{pascal_case ../entry_type.name}}.{{field_name}} || [(await client.create{{pascal_case linked_from.name}}(await sample{{pascal_case linked_from.name}}(client))).actionHash],
{{else}}
{{field_name}}: partial{{pascal_case ../entry_type.name}}.{{field_name}} || [(await client.create{{pascal_case linked_from.name}}(await sample{{pascal_case linked_from.name}}(client))).entryHash],
{{/if}}
{{/if}}
{{else}}
{{#if (eq (pascal_case linked_from.name) (pascal_case ../entry_type.name))}}
{{field_name}}: null,
{{else}}
{{#if (eq linked_from.hash_type "ActionHash")}}
{{field_name}}: partial{{pascal_case ../entry_type.name}}.{{field_name}} || (await client.create{{pascal_case linked_from.name}}(await sample{{pascal_case linked_from.name}}(client))).actionHash,
{{else}}
{{field_name}}: partial{{pascal_case entry_type.name}}.{{field_name}} || (await client.create{{pascal_case linked_from.name}}(await sample{{pascal_case linked_from.name}}(client))).entryHash,
{{/if}}
{{/if}}
{{/if}}
{{else}}
{{field_name}}: client.client.myPubKey,
{{/if}}
{{else}}
{{#if (eq cardinality "vector")}}
{{field_name}}: [{{> (concat field_type.type "/sample-js") field_type=field_type}}],
{{else}}
{{field_name}}: {{> (concat field_type.type "/sample-js") field_type=field_type}},
{{/if}}
{{/if}}
{{/each}}
},
...partial{{pascal_case entry_type.name}}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { toPromise } from '@holochain-open-dev/stores';
import { setup } from './common.js';

{{#if (ne from_referenceable.hash_type "AgentPubKey")}}
import { {{pascal_case from_referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}types.js';
import { sample{{pascal_case from_referenceable.name}} } from './common.js';
import { {{pascal_case from_referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/types.js';
import { sample{{pascal_case from_referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/mocks.js';
{{/if}}
{{#if (ne to_referenceable.hash_type "AgentPubKey")}}
import { {{pascal_case to_referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}types.js';
import { sample{{pascal_case to_referenceable.name}} } from './common.js';
import { {{pascal_case to_referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/types.js';
import { {{pascal_case to_referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/mocks.js';
{{/if}}

test('link a {{pascal_case from_referenceable.name}} to a {{pascal_case to_referenceable.name}}', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{#merge_scope previous_file_content (concat "export class " (pascal_case app_name) "ZomeMock extends ZomeMock implements AppAgentClient {" ) }}
{{previous_scope_content}}

/** {{title_case (plural to_referenceable.name)}} for {{title_case from_referenceable.name}} */
{{camel_case from_referenceable.name}}To{{pascal_case to_referenceable.name}} = new HoloHashMap<{{from_referenceable.hash_type}}, {{to_referenceable.hash_type}}[]>();
{{#if bidireccional}}
{{camel_case to_referenceable.name}}To{{pascal_case from_referenceable.name}} = new HoloHashMap<{{to_referenceable.hash_type}}, {{from_referenceable.hash_type}}[]>();
{{/if}}

{{#if (eq to_referenceable.hash_type "AgentPubKey")}}
async get_{{snake_case (plural to_referenceable.name)}}_for_{{snake_case from_referenceable.name}}({{camel_case from_referenceable.singular_arg}}: {{from_referenceable.hash_type}}): Promise<Array<AgentPubKey>> {
return this.{{camel_case from_referenceable.name}}To{{pascal_case to_referenceable.name}}.get({{camel_case from_referenceable.singular_arg}}) || []
}
{{else}}
async get_{{snake_case (plural to_referenceable.name)}}_for_{{snake_case from_referenceable.name}}({{camel_case from_referenceable.singular_arg}}: {{from_referenceable.hash_type}}): Promise<Array<Record>> {
const actionHashes: {{to_referenceable.hash_type}}[] = this.{{camel_case from_referenceable.name}}To{{pascal_case to_referenceable.name}}.get({{camel_case from_referenceable.singular_arg}}) || [];
return actionHashes.map(actionHash => this.{{camel_case to_referenceable.name}}.entryRecord(actionHash)).map(er => er?.record).filter(r => !!r) as Record[];
}
{{/if}}

async add_{{snake_case to_referenceable.name}}_for_{{snake_case from_referenceable.name}}(input: { {{snake_case from_referenceable.singular_arg}}: {{from_referenceable.hash_type}}; {{snake_case to_referenceable.singular_arg}}: {{to_referenceable.hash_type}} }): Promise<void> {
const existing = this.{{camel_case from_referenceable.name}}To{{pascal_case to_referenceable.name}}.get(input.{{snake_case from_referenceable.singular_arg}}) || [];
this.{{camel_case from_referenceable.name}}To{{pascal_case to_referenceable.name}}.set(input.{{snake_case from_referenceable.singular_arg}}, [...existing, input.{{snake_case to_referenceable.singular_arg}}]);
{{#if bidireccional}}
const existingReverse = this.{{camel_case to_referenceable.name}}To{{pascal_case from_referenceable.name}}.get(input.{{snake_case to_referenceable.singular_arg}}) || [];
this.{{camel_case to_referenceable.name}}To{{pascal_case from_referenceable.name}}.set(input.{{snake_case to_referenceable.singular_arg}}, [...existingReverse, input.{{snake_case from_referenceable.singular_arg}}]);
{{/if}}
}

{{#if bidireccional}}
{{#if (eq from_referenceable.hash_type "AgentPubKey")}}
async get_{{snake_case (plural from_referenceable.name)}}_for_{{snake_case to_referenceable.name}}({{camel_case to_referenceable.singular_arg}}: {{to_referenceable.hash_type}}): Promise<Array<AgentPubKey>> {
return this.{{camel_case to_referenceable.name}}To{{pascal_case from_referenceable.name}}.get({{camel_case to_referenceable.singular_arg}}) || []
}
{{else}}
async get_{{snake_case (plural from_referenceable.name)}}_for_{{snake_case to_referenceable.name}}({{camel_case to_referenceable.singular_arg}}: {{to_referenceable.hash_type}}): Promise<Array<Record>> {
const actionHashes: {{from_referenceable.hash_type}}[] = this.{{camel_case to_referenceable.name}}To{{pascal_case from_referenceable.name}}.get({{camel_case to_referenceable.singular_arg}}) || [];
return actionHashes.map(actionHash => this.{{camel_case from_referenceable.name}}.entryRecord(actionHash)).map(er => er?.record).filter(r => !!r) as Record[];;
}
{{/if}}
{{/if}}

{{/merge_scope}}
4 changes: 3 additions & 1 deletion .templates/app/web-app/Cargo.toml.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ members = ["dnas/*/zomes/coordinator/*", "dnas/*/zomes/integrity/*"]
resolver = "2"

[workspace.dependencies]
holochain = "{{holochain_version}}"
holochain = { version = "{{holochain_version}}", default-features = false, features = [
"test_utils",
] }
hdk = "0.2"
hdi = "0.3"
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { {{pascal_case app_name}}ZomeMock, sample{{pascal_case referenceable.nam
import { {{pascal_case app_name}}Store, {{pascal_case app_name}}Client } from "@holochain-open-dev/{{kebab_case app_name}}";

const mock = new {{pascal_case app_name}}ZomeMock();
const client = new {{pascal_case app_name}}Client(mock);

const {{camel_case referenceable.name}} = await sample{{pascal_case referenceable.name}}();
const {{camel_case referenceable.name}} = await sample{{pascal_case referenceable.name}}(client);

const record = await mock.create_{{snake_case referenceable.name}}({{camel_case referenceable.name}});

const store = new {{pascal_case app_name}}Store(new {{pascal_case app_name}}Client(mock));
const store = new {{pascal_case app_name}}Store(client);

// More on how to set up stories at: https://storybook.js.org/docs/7.0/web-components/writing-stories/introduction
export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { EntryRecord } from '@holochain-open-dev/utils';
import { toPromise } from '@holochain-open-dev/stores';

import { {{pascal_case referenceable.name}} } from '../../ui/src/types.js';
import { setup, sample{{pascal_case referenceable.name}} } from './common.js';
import { sample{{pascal_case referenceable.name}} } from '../../ui/src/mocks.js';
import { setup } from './common.js';

test('create a {{pascal_case referenceable.name}} and get {{lower_case collection_name}}', async () => {
await runScenario(async scenario => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
fixt = "*"
futures = { version = "0.3.1", default-features = false }
hdk = { workspace = true, features = ["encoding", "test_utils"] }
holochain = { version = "0.2", default-features = false, features = [
"test_utils",
] }
holochain = { workspace = true }
tokio = { version = "1.3", features = ["full"] }

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { {{pascal_case ../app_name}}ZomeMock, sample{{pascal_case ../entry_type.
import { {{pascal_case ../app_name}}Store, {{pascal_case ../app_name}}Client } from "@holochain-open-dev/{{kebab_case ../app_name}}";

const mock = new {{pascal_case ../app_name}}ZomeMock();
const client = new {{pascal_case ../app_name}}Client(mock);

const {{camel_case ../entry_type.name}} = await sample{{pascal_case ../entry_type.name}}();
const {{camel_case ../entry_type.name}} = await sample{{pascal_case ../entry_type.name}}(client);

const record = await mock.create_{{snake_case ../entry_type.name}}({{camel_case ../entry_type.name}});

const store = new {{pascal_case ../app_name}}Store(new {{pascal_case ../app_name}}Client(mock));
const store = new {{pascal_case ../app_name}}Store(client);

// More on how to set up stories at: https://storybook.js.org/docs/7.0/web-components/writing-stories/introduction
export default {
Expand Down
Loading

0 comments on commit d1e83d6

Please sign in to comment.