Verified Commit cef9e3df authored by Coby Sher's avatar Coby Sher
Browse files

Issue #3280391 add anon to getObjectByPath, test and update example server

parent d995c7ed
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -388,6 +388,7 @@ class DrupalState {
      res,
      params,
      refresh,
      anon,
    });
    return object;
  }
+1 −1
Original line number Diff line number Diff line
@@ -458,7 +458,7 @@ describe('drupalState', () => {
    expect(fetchMock).toBeCalledTimes(2);
  });

  test('should fetch resource anonymously', async () => {
  test('should fetch resource anonymously when using anon:true', async () => {
    const store: DrupalState = new DrupalState({
      apiBase: 'https://dev-ds-demo.pantheonsite.io',
      apiPrefix: 'jsonapi',
+32 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ const dsConfig = {
  apiBase: 'https://dev-ds-demo.pantheonsite.io',
  apiPrefix: 'jsonapi',
  defaultLocale: 'en',
  debug: true,
};

describe('getObjectByPath', () => {
@@ -162,4 +163,35 @@ describe('getObjectByPath', () => {
        Server responded with status code: 404`);
      });
  });
  test('should fetch a resource anonymously when anon: true', async () => {
    const store: DrupalState = new DrupalState({
      ...dsConfig,
      clientId: '9adc9c69-fa3b-4c21-9cef-fbd345d1a269',
      clientSecret: 'mysecret',
    });
    const getAuthHeaderSpy = jest.spyOn(store, 'getAuthHeader');
    store.setState({ dsApiIndex: demoApiIndex.links });
    fetchMock.mock(
      'https://dev-ds-demo.pantheonsite.io/router/translate-path?path=/recipes/fiery-chili-sauce&_format=json',
      {
        status: 200,
        body: translatePathResponse,
      }
    );
    fetchMock.mock(
      'https://dev-ds-demo.pantheonsite.io/en/jsonapi/node/recipe/da1359f4-2e60-462c-8909-47c3bce11fdf',
      {
        status: 200,
        body: recipesResourceData,
      }
    );
    expect(
      await store.getObjectByPath({
        objectName: 'node--recipe',
        path: '/recipes/fiery-chili-sauce',
        anon: true,
      })
    ).toEqual(recipesResourceByPath);
    expect(getAuthHeaderSpy).toBeCalledTimes(0);
  });
});
+16 −0
Original line number Diff line number Diff line
@@ -187,6 +187,22 @@ async function main(): Promise<void> {
  //   })
  // );

  // console.log('-- Fetch an object anonymously --');
  // console.log(
  //   await authStore.getObject({
  //     objectName: 'node-recipe',
  //     anon: true,
  //   })
  // );

  // console.log('-- Also works with getObjectByPath --');
  // console.log(
  //   await authStore.getObjectByPath({
  //     objectName: 'node-recipe',
  //     path: '/recipes/fiery-chili-sauce',
  //     anon: true,
  //   })
  // );
  // You also have direct access to the Zustand store if necessary
  store.setState({ custom: 'custom state' });
}