Skip to content

Commit

Permalink
add test in jset
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuchi committed Aug 9, 2024
1 parent eb87404 commit ad1a52a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/redis/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
"value"
],
"docs": {
"description": "Creates a JSON object at the key",
"description": "Creates a JSON object at the key\nThis function will overwrites the values for the specified key with the new object",
"tags": [
{
"title": "example",
Expand Down Expand Up @@ -450,7 +450,7 @@
"options"
],
"docs": {
"description": "Returns all keys which match the provided pattern.\nscan iterates the whole database to find the matching keys",
"description": "Returns all keys which patch the provided pattern.",
"tags": [
{
"title": "example",
Expand Down
63 changes: 56 additions & 7 deletions packages/redis/test/Adaptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,59 @@ describe('hGetAll', () => {
});
});

describe('jGet', () => {
const jGetClient = {
json: {
get: async key => {
expect(key).to.eql('animals:1');
return {
name: 'Fluffy',
species: 'cat',
age: '3',
};
},
},
};
it('should expand references', async () => {
setMockClient(jGetClient);
const state = { key: 'animals:1' };
const result = await jGet(s => s.key)(state);
expect(result.data).to.eql({
name: 'Fluffy',
species: 'cat',
age: '3',
});
});
it('should throw if key not specified', async () => {
setMockClient({
json: {
get: async () => {
throw new Error();
},
},
});

const state = {};
try {
await jGet()(state);
} catch (error) {
expect(error.message).to.eql('TypeError: Invalid argument type');
expect(error.code).to.eql('ARGUMENT_ERROR');
}
});
it('should get JSON document of specified key', async () => {
setMockClient(jGetClient);

const state = {};
const result = await jGet('animals:1')(state);
expect(result.data).to.eql({
name: 'Fluffy',
species: 'cat',
age: '3',
});
});
});

describe('jSet', () => {
const jSetClient = {
json: {
Expand Down Expand Up @@ -494,7 +547,7 @@ describe('jSet', () => {
}
});

it.only('should always sets at the document root', async () => {
it('should always sets at the document root', async () => {
setMockClient({
json: {
set: () => 'OK',
Expand All @@ -506,11 +559,7 @@ describe('jSet', () => {

const state = {};
await jSet('animals:1', { name: 'mammoth' })(state);

const result = jGet('animals:1')(state);

console.log({ result });

expect(result.data).to.eql({ a });
const result = await jGet('animals:1')(state);
expect(result.data).to.eql({ name: 'mammoth' });
});
});

0 comments on commit ad1a52a

Please sign in to comment.