<commit_before>/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @format
 * @flow strict-local
 * @emails oncall+relay
 */

// flowlint ambiguous-object-type:error

'use strict';

const RelayModernEnvironment = require('../RelayModernEnvironment');
const RelayModernStore = require('../RelayModernStore');
const RelayNetwork = require('../../network/RelayNetwork');
const RelayRecordSource = require('../RelayRecordSource');

const {graphql, getFragment, getRequest} = require('../../query/GraphQLTag');
const {
  createOperationDescriptor,
} = require('../RelayModernOperationDescriptor');
const {createReaderSelector} = require('../RelayModernSelector');
const {disallowWarnings} = require('relay-test-utils-internal');

disallowWarnings();

describe('commitUpdate()', () => {
  let environment;
  let operation;
  let ParentQuery;
  let source;
  let store;
  let UserFragment;

  beforeEach(() => {
    jest.resetModules();

    ParentQuery = getRequest(graphql`
      query RelayModernEnvironmentCommitUpdateTestParentQuery {
        me {
          id
          name
        }
      }
    `);
    UserFragment = getFragment(graphql`
      fragment RelayModernEnvironmentCommitUpdateTestUserFragment on User {
        id
        name
      }
    `);

    source = RelayRecordSource.create();
    store = new RelayModernStore(source);
    environment = new RelayModernEnvironment({
      network: RelayNetwork.create(jest.fn()),
      store,
    });
    operation = createOperationDescriptor(ParentQuery, {});
  });

  it('applies the update to the store', () => {
    const selector = createReaderSelector(
      UserFragment,
      '4',
      {},
      operation.request,
    );
    const callback = jest.fn();
    const snapshot = environment.lookup(selector);
    environment.subscribe(snapshot, callback);

    environment.commitUpdate(proxyStore => {
      const zuck = proxyStore.create('4', 'User');
      zuck.setValue('4', 'id');
      zuck.setValue('zuck', 'name');
    });
    expect(callback.mock.calls.length).toBe(1);
    expect(callback.mock.calls[0][0].data).toEqual({
      id: '4',
      name: 'zuck',
    });
  });

  describe('when using a scheduler', () => {
    let taskID;
    let tasks;
    let scheduler;
    let runTask;

    beforeEach(() => {
      taskID = 0;
      tasks = new Map();
      scheduler = {
        cancel: id => {
          tasks.delete(id);
        },
        schedule: task => {
          const id = String(taskID++);
          tasks.set(id, task);
          return id;
        },
      };
      runTask = () => {
        for (const [id, task] of tasks) {
          tasks.delete(id);
          task();
          break;
        }
      };
      environment = new RelayModernEnvironment({
        network: RelayNetwork.create(jest.fn()),
        scheduler,
        store,
      });
    });

    it('applies the mutation to the store', () => {
      const selector = createReaderSelector(
        UserFragment,
        '4',
        {},
        operation.request,
      );
      const callback = jest.fn();
      const snapshot = environment.lookup(selector);
      environment.subscribe(snapshot, callback);

      environment.commitUpdate(proxyStore => {
        const zuck = proxyStore.create('4', 'User');
        zuck.setValue('4', 'id');
        zuck.setValue('zuck', 'name');
      });

      // Verify task was scheduled and run it
      expect(tasks.size).toBe(1);
      runTask();

      // Update is applied after scheduler runs scheduled tas
      expect(callback.mock.calls.length).toBe(1);
      expect(callback.mock.calls[0][0].data).toEqual({
        id: '4',
        name: 'zuck',
      });
    });
  });
});<commit_msg>Add MAE to RelayModernEnvironment-CommitUpdate-test.js<commit_after>/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @format
 * @flow strict-local
 * @emails oncall+relay
 */

// flowlint ambiguous-object-type:error

'use strict';

const RelayModernEnvironment = require('../RelayModernEnvironment');
const RelayModernStore = require('../RelayModernStore');
const RelayNetwork = require('../../network/RelayNetwork');
const RelayRecordSource = require('../RelayRecordSource');

const {
  getActorIdentifier,
  MultiActorEnvironment,
} = require('../../multi-actor-environment');
const {graphql, getFragment, getRequest} = require('../../query/GraphQLTag');
const {
  createOperationDescriptor,
} = require('../RelayModernOperationDescriptor');
const {createReaderSelector} = require('../RelayModernSelector');
const {disallowWarnings} = require('relay-test-utils-internal');

disallowWarnings();

describe.each(['RelayModernEnvironment', 'MultiActorEnvironment'])(
  'commitUpdate()',
  environmentType => {
    let environment;
    let operation;
    let ParentQuery;
    let source;
    let store;
    let UserFragment;

    describe(environmentType, () => {
      beforeEach(() => {
        ParentQuery = getRequest(graphql`
          query RelayModernEnvironmentCommitUpdateTestParentQuery {
            me {
              id
              name
            }
          }
        `);
        UserFragment = getFragment(graphql`
          fragment RelayModernEnvironmentCommitUpdateTestUserFragment on User {
            id
            name
          }
        `);

        source = RelayRecordSource.create();
        store = new RelayModernStore(source);
        const fetch = jest.fn();
        const multiActorEnvironment = new MultiActorEnvironment({
          createNetworkForActor: _actorID => RelayNetwork.create(fetch),
          createStoreForActor: _actorID => store,
        });
        environment =
          environmentType === 'MultiActorEnvironment'
            ? multiActorEnvironment.forActor(getActorIdentifier('actor:1234'))
            : new RelayModernEnvironment({
                network: RelayNetwork.create(fetch),
                store,
              });
        operation = createOperationDescriptor(ParentQuery, {});
      });

      it('applies the update to the store', () => {
        const selector = createReaderSelector(
          UserFragment,
          '4',
          {},
          operation.request,
        );
        const callback = jest.fn();
        const snapshot = environment.lookup(selector);
        environment.subscribe(snapshot, callback);

        environment.commitUpdate(proxyStore => {
          const zuck = proxyStore.create('4', 'User');
          zuck.setValue('4', 'id');
          zuck.setValue('zuck', 'name');
        });
        expect(callback.mock.calls.length).toBe(1);
        expect(callback.mock.calls[0][0].data).toEqual({
          id: '4',
          name: 'zuck',
        });
      });

      describe('when using a scheduler', () => {
        let taskID;
        let tasks;
        let scheduler;
        let runTask;

        beforeEach(() => {
          taskID = 0;
          tasks = new Map();
          scheduler = {
            cancel: id => {
              tasks.delete(id);
            },
            schedule: task => {
              const id = String(taskID++);
              tasks.set(id, task);
              return id;
            },
          };
          runTask = () => {
            for (const [id, task] of tasks) {
              tasks.delete(id);
              task();
              break;
            }
          };
          environment = new RelayModernEnvironment({
            network: RelayNetwork.create(jest.fn()),
            scheduler,
            store,
          });
        });

        it('applies the mutation to the store', () => {
          const selector = createReaderSelector(
            UserFragment,
            '4',
            {},
            operation.request,
          );
          const callback = jest.fn();
          const snapshot = environment.lookup(selector);
          environment.subscribe(snapshot, callback);

          environment.commitUpdate(proxyStore => {
            const zuck = proxyStore.create('4', 'User');
            zuck.setValue('4', 'id');
            zuck.setValue('zuck', 'name');
          });

          // Verify task was scheduled and run it
          expect(tasks.size).toBe(1);
          runTask();

          // Update is applied after scheduler runs scheduled tas
          expect(callback.mock.calls.length).toBe(1);
          expect(callback.mock.calls[0][0].data).toEqual({
            id: '4',
            name: 'zuck',
          });
        });
      });
    });
  },
);
{'fraction_non_alphanumeric': 0.13285829215032754, 'fraction_numerical': 0.004137455464889093, 'mean_word_length': 2.1643636363636363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6003492', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2332, 'n_words': 514}