@line/liff-mock
Version:
Make testing your LIFF app easy
139 lines (99 loc) • 2.9 kB
Markdown
LIFF Mock is a LIFF Plugin that make testing your LIFF app easy.
**※ LIFF Plugin feature is available since LIFF SDK v2.19.0.**
```sh
$ npm install @line/liff-mock
```
```ts
import liff from '@line/liff';
import { LiffMockPlugin } from '@line/liff-mock';
liff.use(new LiffMockPlugin());
liff.init({
liffId: 'liff-xxxx',
mock: true, // enable mock mode
});
if (!liff.isInClient()) liff.login();
const profile = await liff.getProfile();
// { displayName: 'Brown', userId: '123456789', statusMessage: 'hello' }
console.log(profile);
```
If you use LIFF Mock with Pluggable SDK mode, you have to install `IsInClientModule` before the installation of `LiffMockPlugin` because LIFF Mock depends on `liff.isInClient` API.
```ts
import liff from '@line/liff/core';
import IsInClientModule from '@line/liff/is-in-client';
import { LiffMockPlugin } from '@line/liff-mock';
liff.use(new IsInClientModule()); // <-- Please install IsInClientModule before LiffMockPlugin
liff.use(new LiffMockPlugin());
liff.init({
liffId: 'liff-xxxx',
mock: true, // enable mock mode
});
if (!liff.isInClient()) liff.login();
const profile = await liff.getProfile();
// { displayName: 'Brown', userId: '123456789', statusMessage: 'hello' }
console.log(profile);
```
https://unpkg.com/@line/liff-mock@1.0.4/dist/umd/liff-mock.js
```html
<!-- in <head> tag -->
<script src="https://unpkg.com/@line/liff-mock@1.0.4/dist/umd/liff-mock.js"></script>
```
```js
const liff = window.liff;
const liffMockPlugin = window.liffMock;
liff.use(new LiffMockPlugin());
liff.init({
liffId: 'liff-xxxx',
mock: true, // enable mock mode
});
if (!liff.isInClient()) liff.login();
const profile = await liff.getProfile();
// { displayName: 'Brown', userId: '123456789', statusMessage: 'hello' }
console.log(profile);
```
Set mock data
```ts
type set = (
data: Partial<MockData> | ((prev: Partial<MockData>) => Partial<MockData>)
) => void;
```
```js
const profile = await liff.getProfile();
// { displayName: 'Brown', userId: '123456789', statusMessage: 'hello' }
console.log(profile);
liff.$mock.set((p) => ({
...p,
getProfile: { displayName: 'Cony', userId: '1111111' },
}));
const profile = await liff.getProfile();
// { displayName: 'Cony', userId: '1111111' }
console.log(profile);
```
Restore default mock data
```ts
type clear: () => void;
```
```js
// liff.$mock.set(...)
const profile = await liff.getProfile();
// { displayName: 'Cony', userId: '1111111' }
console.log(profile);
liff.$mock.clear();
const profile = await liff.getProfile();
// { displayName: 'Brown', userId: '123456789', statusMessage: 'hello' }
console.log(profile);
```
See [examples](./examples)
```sh
$ nvm use
$ npm ci
```