|
| 1 | +import { describe, it, expect } from '#ketting-test'; |
| 2 | + |
| 3 | +import { Resource, Links } from 'ketting'; |
| 4 | +import { Resources } from '#ketting-dist/state/resources.js'; |
| 5 | + |
| 6 | +describe('Resources', () => { |
| 7 | + |
| 8 | + it('should behave like an Array', () => { |
| 9 | + |
| 10 | + const fakeResources = [getFakeResource('/a'), getFakeResource('/b')]; |
| 11 | + const resources = new Resources(fakeResources); |
| 12 | + |
| 13 | + expect(resources).to.be.an.instanceof(Array); |
| 14 | + const uris = resources.map(r => r.uri); |
| 15 | + expect(uris).to.eql(['/a', '/b']); |
| 16 | + |
| 17 | + resources.push(getFakeResource('/c')); |
| 18 | + |
| 19 | + expect(resources[2].uri).to.eql('/c'); |
| 20 | + |
| 21 | + }); |
| 22 | + |
| 23 | + it('should resolve all resources to their state when get() is called', async () => { |
| 24 | + |
| 25 | + const fakeResources = [getFakeResource('/a'), getFakeResource('/b')]; |
| 26 | + const resources = new Resources(fakeResources); |
| 27 | + |
| 28 | + const states = await resources.get(); |
| 29 | + expect(states).to.have.length(2); |
| 30 | + expect(states[0].uri).to.eql('/a'); |
| 31 | + expect(states[1].uri).to.eql('/b'); |
| 32 | + |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return an empty array from get() when there are no resources', async () => { |
| 36 | + |
| 37 | + const resources = new Resources([]); |
| 38 | + const states = await resources.get(); |
| 39 | + expect(states).to.eql([]); |
| 40 | + |
| 41 | + }); |
| 42 | + |
| 43 | +}); |
| 44 | + |
| 45 | +function getFakeResource(uri: string = 'https://example.org/') { |
| 46 | + |
| 47 | + const fakeFetch = (input:any) => { |
| 48 | + let url; |
| 49 | + if (input.url) { |
| 50 | + url = input.url; |
| 51 | + } else { |
| 52 | + url = input; |
| 53 | + } |
| 54 | + switch(url) { |
| 55 | + case 'https://example.org/return-request': |
| 56 | + return input; |
| 57 | + case 'https://example.org/200': |
| 58 | + return new Response('', {status: 200}); |
| 59 | + case 'https://example.org/201': |
| 60 | + return new Response('', {status: 201}); |
| 61 | + case 'https://example.org/201-loc': |
| 62 | + return new Response('', {status: 201, headers: { 'Location': 'https://evertpot.com/'}}); |
| 63 | + case 'https://example.org/205': |
| 64 | + return new Response(null, {status: 205}); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + const fakeClient:any = { |
| 69 | + |
| 70 | + fetcher: { |
| 71 | + |
| 72 | + fetch: fakeFetch, |
| 73 | + fetchOrThrow: fakeFetch, |
| 74 | + }, |
| 75 | + |
| 76 | + go: (uri:string) => { |
| 77 | + |
| 78 | + return getFakeResource(uri); |
| 79 | + |
| 80 | + }, |
| 81 | + |
| 82 | + cache: { |
| 83 | + |
| 84 | + get: ():null => { return null; }, |
| 85 | + store: ():void => { /* Intentionally Empty */ } |
| 86 | + |
| 87 | + }, |
| 88 | + |
| 89 | + cacheState: (state: any) => { |
| 90 | + |
| 91 | + return; |
| 92 | + |
| 93 | + }, |
| 94 | + |
| 95 | + getStateForResponse: () => { |
| 96 | + |
| 97 | + return { |
| 98 | + uri, |
| 99 | + links: new Links('/', [ { href: '/', rel: 'yes', context: '/' }]) |
| 100 | + }; |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + }; |
| 105 | + const resource = new Resource(fakeClient, uri); |
| 106 | + return resource; |
| 107 | + |
| 108 | +} |
0 commit comments