Skip to content

Commit

Permalink
feat(shared data): allow sharing calling shared data multiple times
Browse files Browse the repository at this point in the history
thanks @arthur-er !
  • Loading branch information
eidellev committed Jun 21, 2022
1 parent 51f85ee commit 9c79402
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Inertia implements InertiaContract {
constructor(private app: ApplicationContract, private ctx: HttpContextContract, private config: InertiaConfig) {}

public static share(data: SharedData) {
Inertia.sharedData = data;
Inertia.sharedData = { ...Inertia.sharedData, ...data };
return Inertia;
}

Expand Down
32 changes: 31 additions & 1 deletion test/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { setup, teardown } from './utils';
test.group('Data', (group) => {
group.afterEach(async () => {
await teardown();
Inertia.share({});
// @ts-ignore
Inertia.sharedData = {};
});

test('Should return shared data', async (assert) => {
Expand Down Expand Up @@ -40,6 +41,35 @@ test.group('Data', (group) => {
});
});

test('Should combine shared data(multiple calls)', async (assert) => {
const props = {
some: {
props: {
for: ['your', 'page'],
},
},
};
const app = await setup();
const server = createServer(async (req, res) => {
const ctx = app.container.use('Adonis/Core/HttpContext').create('/', {}, req, res);
Inertia.share({
shared: 'data',
}).share({ additional: 'shared data' });
const response = await ctx.inertia.render('Some/Page', props);

res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify(response));
res.end();
});

const response = await supertest(server).get('/').set(HEADERS.INERTIA_HEADER, 'true').expect(200);
assert.deepEqual(response.body, {
component: 'Some/Page',
props: { ...props, shared: 'data', additional: 'shared data' },
url: '/',
});
});

test('Should resolve lazy props', async (assert) => {
const props = {
some() {
Expand Down

0 comments on commit 9c79402

Please sign in to comment.