0.1.10 • Published 2 months ago

mock-block-dock v0.1.10

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Mock Block Dock

Mock datastore and embedding application for testing Block Protocol blocks.

yarn add mock-block-dock

MockBlockDock is automatically included the template generated using create-block-app

See the docs for help with the fundamental Block Protocol concepts Block Protocol

Usage

There are two options for usage:

  1. Use the component MockBlockDock, and pass it your block source, to have it automatically:
  • render your block
  • pass it its starting properties
  • listen for messages from the block, updates the datastore and respond appropriately
  1. Use the hook useMockBlockProps, which returns the mock data, properties and message callbacks for your custom use.

Component: mock embedding application

import { MockBlockDock } from "mock-block-dock";

The component will automatically load your block, pass it properties (where it accepts properties), and listen for messages from the block, responding accordingly.

The properties passed are described in the full Block Protocol documentation((https://blockprotocol.org/docs).

For example, if you call updateEntity, MockBlockDock will update the specified entity and return it. If the entity is part of the properties sent to your block, they will be updated and the block re-rendered.

const { data, errors } = await graphModule.updateEntity({
  entityId,
  properties: newProps,
});

There are different ways of loading your block source depending on the entry point.

For each, you can set the block's starting entity by providing it among initialData.initialEntities and identifying it via the blockEntityRecordId prop (see examples below))

const blockEntity: Entity = {
  metadata: {
    recordId: {
      entityId: "entity-1", // the entity's unique, stable identifier
      editionId: new Date(0).toISOString(), // the specific edition/version identifier
    },
    entityTypeId: "https://example.com/my-types/entity-types/person/v/1",
  },
  properties: { "https://example.com/my-types/property-type/name/": "World" },
};

React

When developing a React block, pass your component

import { MockBlockDock } from "mock-block-dock";

import MyBlock from "./my-block.tsx";

<MockBlockDock
  blockDefinition={{ ReactComponent: MyBlock }} // provide the block source code
  blockEntityRecordId={blockEntity.metadata.recordId} // identifies the block entity among the initial entities
  debug // show the debug UI on startup
  initialData={{
    initialEntities: [blockEntity], // initial entities to load into the mock datastore
  }}
/>;

Custom element / Web Component

When developing a custom element block, pass MockBlockDock the class and the desired tag name.

import { MockBlockDock } from "mock-block-dock";

import MyCustomElement from "./my-custom-element.ts";

<MockBlockDock
  blockDefinition={{
    customElement: { elementClass: MyCustomClass, tagName: "my-block" },
  }}
  blockEntityRecordId={blockEntity.metadata.recordId} // identifies the block entity among the initial entities
  debug // show the debug UI on startup
  initialData={{
    initialEntities: [blockEntity], // initial entities to load into the mock datastore
  }}
/>;

Debug mode

When passed debug={true} (which can be shortened to debug), MockBlockDock will also render a display of:

  1. the properties being passed to the blocks (for non-HTML blocks), which are derived from the mock datastore
  2. the raw contents of the mock datastore

Readonly mode

When passed readonly={true} (which can be shortened to readonly), MockBlockDock will send a readonly: true message to the block, and will reject any attempts to mutate data in the datastore.

You can also toggle this option in the debug UI.

Hook: custom, manual control

If you want more control or visibility over the mock properties, you can retrieve them as a hook instead, and pass them to your block yourself.

You should pass initialData and blockEntityRecordId to set the starting datastore and identify the block entity.

import { useMockBlockProps } from "mock-block-dock";

const {
  blockProperties,
  blockProtocolFunctions,
  entityTypes,
  linkedQueries,
  linkedEntities,
  linkGroups,
} = useMockBlockProps({
  blockEntityRecordId: blockEntity.metadata.recordId,
  initialData: { initialEntities: [blockEntity] },
});
0.1.10

2 months ago

0.1.8

3 months ago

0.1.9

3 months ago

0.1.7

4 months ago

0.1.6

11 months ago

0.1.5

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.1.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.0.37

2 years ago

0.0.38

2 years ago

0.0.33

2 years ago

0.0.34

2 years ago

0.0.35

2 years ago

0.0.36

2 years ago

0.0.37-canary.0

2 years ago

0.0.37-canary.1

2 years ago

0.0.37-canary.2

2 years ago

0.0.37-canary.3

2 years ago

0.0.37-canary.4

2 years ago

0.0.36-canary.5

2 years ago

0.0.36-canary.4

2 years ago

0.0.36-canary.1

2 years ago

0.0.36-canary.0

2 years ago

0.0.36-canary.2

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.23-canary.4

2 years ago

0.0.23-canary.5

2 years ago

0.0.23-canary.2

2 years ago

0.0.23-canary.3

2 years ago

0.0.23-canary.0

2 years ago

0.0.23-canary.1

2 years ago

0.0.16-next.0

2 years ago

0.0.14-next.0

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.14-next.1

2 years ago

0.0.11-beta

2 years ago

0.0.18-canary.0

2 years ago

0.0.18-canary.1

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.13-beta

2 years ago

0.0.10

2 years ago

0.0.14

2 years ago

0.0.17-canary.4

2 years ago

0.0.17-canary.3

2 years ago

0.0.12-beta

2 years ago

0.0.17-canary.0

2 years ago

0.0.17-canary.2

2 years ago

0.0.17-canary.1

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago