|
| 1 | +import StoryblokClient from 'storyblok-js-client'; |
| 2 | +// TODO |
| 3 | +import { describe, expect, it } from '../../../test-utils/src/unit-test/utils'; |
| 4 | +import { hasStories, makeStory } from '../../../test-utils/src/preconditions/stories'; |
| 5 | + |
| 6 | +const makeMapi = ({ baseURL }: { baseURL: string }) => new StoryblokClient({ |
| 7 | + oauthToken: 'Bearer super-valid-token', |
| 8 | + endpoint: `${baseURL}/v1`, |
| 9 | +}); |
| 10 | + |
| 11 | +describe('getAll()', () => { |
| 12 | + for (let i = 0; i < 100; i++) { |
| 13 | + it(`${i}: should return a list of stories`, async ({ prepare, stubServer }) => { |
| 14 | + const mapi = makeMapi(stubServer); |
| 15 | + |
| 16 | + await prepare(hasStories({ spaceId: '123', stories: [] })); |
| 17 | + const resultEmpty = await mapi.getAll( |
| 18 | + `spaces/123/stories`, |
| 19 | + ); |
| 20 | + expect(resultEmpty.length).toBe(0); |
| 21 | + |
| 22 | + const story = makeStory({ name: 'foo bar' }); |
| 23 | + await prepare(hasStories({ spaceId: '123', stories: [makeStory({ name: 'foo bar' })] })); |
| 24 | + const result = await mapi.getAll( |
| 25 | + `spaces/123/stories`, |
| 26 | + ); |
| 27 | + expect(result[0].name).toBe(story.name); |
| 28 | + }); |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +// describe('mapi', () => { |
| 33 | + |
| 34 | +// let client: StoryblokClient; |
| 35 | + |
| 36 | +// beforeEach(() => { |
| 37 | +// // Setup default mocks |
| 38 | +// client = new StoryblokClient({ |
| 39 | +// accessToken: process.env.VITE_ACCESS_TOKEN, |
| 40 | +// cache: { type: 'memory', clear: 'auto' }, |
| 41 | +// }); |
| 42 | +// }); |
| 43 | +// // TODO: Uncomment when we have a valid token |
| 44 | +// /* if (process.env.VITE_OAUTH_TOKEN) { |
| 45 | +// describe('management API', () => { |
| 46 | +// const spaceId = process.env.VITE_SPACE_ID |
| 47 | +// describe('should return all spaces', async () => { |
| 48 | +// const StoryblokManagement = new StoryblokClient({ |
| 49 | +// oauthToken: process.env.VITE_OAUTH_TOKEN, |
| 50 | +// }) |
| 51 | +// const result = await StoryblokManagement.getAll( |
| 52 | +// `spaces/${spaceId}/stories` |
| 53 | +// ) |
| 54 | +// expect(result.length).toBeGreaterThan(0) |
| 55 | +// }) |
| 56 | +// }) |
| 57 | +// } */ |
| 58 | + |
| 59 | +// describe('get function', () => { |
| 60 | +// it('get(\'cdn/spaces/me\') should return the space information', async () => { |
| 61 | +// const { data } = await client.get('cdn/spaces/me'); |
| 62 | +// expect(data.space.id).toBe(Number(process.env.VITE_SPACE_ID)); |
| 63 | +// }); |
| 64 | + |
| 65 | +// it('get(\'cdn/stories\') should return all stories', async () => { |
| 66 | +// const { data } = await client.get('cdn/stories'); |
| 67 | +// expect(data.stories.length).toBeGreaterThan(0); |
| 68 | +// }); |
| 69 | + |
| 70 | +// it('get(\'cdn/stories/testcontent-0\' should return the specific story', async () => { |
| 71 | +// const { data } = await client.get('cdn/stories/testcontent-0'); |
| 72 | +// expect(data.story.slug).toBe('testcontent-0'); |
| 73 | +// }); |
| 74 | + |
| 75 | +// it('get(\'cdn/stories\' { starts_with: testcontent-0 } should return the specific story', async () => { |
| 76 | +// const { data } = await client.get('cdn/stories', { |
| 77 | +// starts_with: 'testcontent-0', |
| 78 | +// }); |
| 79 | +// expect(data.stories.length).toBe(1); |
| 80 | +// }); |
| 81 | + |
| 82 | +// it('get(\'cdn/stories/testcontent-draft\', { version: \'draft\' }) should return the specific story draft', async () => { |
| 83 | +// const { data } = await client.get('cdn/stories/testcontent-draft', { |
| 84 | +// version: 'draft', |
| 85 | +// }); |
| 86 | +// expect(data.story.slug).toBe('testcontent-draft'); |
| 87 | +// }); |
| 88 | + |
| 89 | +// it('get(\'cdn/stories/testcontent-0\', { version: \'published\' }) should return the specific story published', async () => { |
| 90 | +// const { data } = await client.get('cdn/stories/testcontent-0', { |
| 91 | +// version: 'published', |
| 92 | +// }); |
| 93 | +// expect(data.story.slug).toBe('testcontent-0'); |
| 94 | +// }); |
| 95 | + |
| 96 | +// it('cdn/stories/testcontent-0 should resolve author relations', async () => { |
| 97 | +// const { data } = await client.get('cdn/stories/testcontent-0', { |
| 98 | +// resolve_relations: 'root.author', |
| 99 | +// }); |
| 100 | + |
| 101 | +// expect(data.story.content.author[0].slug).toBe('edgar-allan-poe'); |
| 102 | +// }); |
| 103 | + |
| 104 | +// it('get(\'cdn/stories\', { by_slugs: \'folder/*\' }) should return the specific story', async () => { |
| 105 | +// const { data } = await client.get('cdn/stories', { |
| 106 | +// by_slugs: 'folder/*', |
| 107 | +// }); |
| 108 | +// expect(data.stories.length).toBeGreaterThan(0); |
| 109 | +// }); |
| 110 | +// }); |
| 111 | + |
| 112 | +// describe('getAll function', () => { |
| 113 | +// it('getAll(\'cdn/stories\') should return all stories', async () => { |
| 114 | +// const result = await client.getAll('cdn/stories', {}); |
| 115 | +// expect(result.length).toBeGreaterThan(0); |
| 116 | +// }); |
| 117 | + |
| 118 | +// it('getAll(\'cdn/stories\') should return all stories with filtered results', async () => { |
| 119 | +// const result = await client.getAll('cdn/stories', { |
| 120 | +// starts_with: 'testcontent-0', |
| 121 | +// }); |
| 122 | +// expect(result.length).toBe(1); |
| 123 | +// }); |
| 124 | + |
| 125 | +// it('getAll(\'cdn/stories\', filter_query: { __or: [{ category: { any_in_array: \'Category 1\' } }, { category: { any_in_array: \'Category 2\' } }]}) should return all stories with the specific filter applied', async () => { |
| 126 | +// const result = await client.getAll('cdn/stories', { |
| 127 | +// filter_query: { |
| 128 | +// __or: [ |
| 129 | +// { category: { any_in_array: 'Category 1' } }, |
| 130 | +// { category: { any_in_array: 'Category 2' } }, |
| 131 | +// ], |
| 132 | +// }, |
| 133 | +// }); |
| 134 | +// expect(result.length).toBeGreaterThan(0); |
| 135 | +// }); |
| 136 | + |
| 137 | +// it('getAll(\'cdn/stories\', {by_slugs: \'folder/*\'}) should return all stories with the specific filter applied', async () => { |
| 138 | +// const result = await client.getAll('cdn/stories', { |
| 139 | +// by_slugs: 'folder/*', |
| 140 | +// }); |
| 141 | +// expect(result.length).toBeGreaterThan(0); |
| 142 | +// }); |
| 143 | + |
| 144 | +// it('getAll(\'cdn/links\') should return all links', async () => { |
| 145 | +// const result = await client.getAll('cdn/links', {}); |
| 146 | +// expect(result.length).toBeGreaterThan(0); |
| 147 | +// }); |
| 148 | +// }); |
| 149 | + |
| 150 | +// describe('caching', () => { |
| 151 | +// it('get(\'cdn/spaces/me\') should not be cached', async () => { |
| 152 | +// const provider = client.cacheProvider(); |
| 153 | +// await provider.flush(); |
| 154 | +// await client.get('cdn/spaces/me'); |
| 155 | +// expect(Object.values(provider.getAll()).length).toBe(0); |
| 156 | +// }); |
| 157 | + |
| 158 | +// it('get(\'cdn/stories\') should be cached when is a published version', async () => { |
| 159 | +// const cacheVersion = client.cacheVersion(); |
| 160 | + |
| 161 | +// await client.get('cdn/stories'); |
| 162 | + |
| 163 | +// expect(cacheVersion).not.toBe(undefined); |
| 164 | + |
| 165 | +// const newCacheVersion = client.cacheVersion(); |
| 166 | + |
| 167 | +// await client.get('cdn/stories'); |
| 168 | + |
| 169 | +// expect(newCacheVersion).toBe(client.cacheVersion()); |
| 170 | + |
| 171 | +// await client.get('cdn/stories'); |
| 172 | + |
| 173 | +// expect(newCacheVersion).toBe(client.cacheVersion()); |
| 174 | +// }); |
| 175 | +// }); |
| 176 | +// }); |
0 commit comments